2012-02-24 68 views
2

我有一個測試動態壁紙,我希望從一項活動開始。目前,當我在模擬器上運行項目時,它立即發現自己處於活動壁紙選擇器中。到處搜索後,我仍然無法找到一種方法,只有在我的主要活動中的按鈕被按下後才顯示出來。Android:你如何從一個按鈕啓動一個wallpaperervice?

我嘗試在我的主要活動中引入一個靜態布爾值,isnotpressed,並在onClick()按鈕中將其設置爲false。然後我使用stopSelf()在我的wallpaperservice類中,而isnotpressed爲true。不幸的是,這些都沒有用,我認爲我無論如何都以正確的方式去解決這個問題。我也嘗試this,但它也沒有工作。任何幫助將在這裏深受讚賞。

這裏的主要活動:

public class TestProjectActivity extends Activity{ 

static boolean isnotPressed; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    isnotPressed = true; 

final Button b1 = (Button) findViewById(R.id.button1); 

b1.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     isnotPressed = false; 
    } 
}); 

} 

}

而這裏的wallpaperservice的一部分:

public class MyWallpaperService extends WallpaperService { 




@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 

    while(TestProjectActivity.isnotPressed){stopSelf();} 


} 

回答

0

試試這個:

Intent i = new Intent(); 
    if (Build.VERSION.SDK_INT > 15) 
    { 
     i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); 
     String pkg = MyPreferencesActivity.class.getPackage().getName(); 
     String cls = MyPreferencesActivity.class.getCanonicalName(); 
     i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls)); 
    } 
    else 
    { 
     i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER); 
    } 
    startActivityForResult(i, 0); 
相關問題