gpt4 book ai didi

android - Activity 不调用服务

转载 作者:行者123 更新时间:2023-11-30 02:22:38 25 4
gpt4 key购买 nike

我是android编程的新手。我正在尝试创建一个动态壁纸应用程序。遵循 Lars Vogella 的教程。我创建了一个 Activity 、一个按钮和一个服务。当我单击按钮时,它会进入 onClick 函数。但它不会启动服务。我真的很困惑。

    public class SetWallpaperActivity extends Activity {
private final String TAG = "SetWallpaperActivity";
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.main);
}

public void onClick(View view){
Toast.makeText(this, "Entered onClick",Toast.LENGTH_LONG).show();
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this,MyWallpaperService.class));
startActivity(intent);
}
}

我的服务类看起来像这样

    public class MyWallpaperService extends WallpaperService {
private final String TAG = "MyWallpaperService";
public Engine onCreateEngine(){
Log.v(TAG, "Creating Engine");
return new RenderEngine();
}

public class RenderEngine extends Engine{
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable(){
public void run(){
draw();
}
};
private Paint paint = new Paint();
private boolean visible = true;
private int width;
int height;


public RenderEngine(){
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10.0f);
handler.post(drawRunner);
}

@Override
public void onVisibilityChanged(boolean visibility){
this.visible = visibility;
if(visibility){
handler.post(drawRunner);
}else{
handler.removeCallbacks(drawRunner);
}
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder){
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
this.width = width;
this.height = height;
super.onSurfaceChanged(holder, format, width, height);
}

public void draw(){
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
Log.v(TAG,"Entered Draw");
try{
c = holder.lockCanvas();
if(c != null){
Log.v(TAG,"Entered Try");
}
}finally{
if(c != null){
holder.unlockCanvasAndPost(c);
}
}
handler.removeCallbacks(drawRunner);
if(visible){
handler.postDelayed(drawRunner,5000);
}
}
}
}

谁能帮我解决一下?我被困在这里3天了。

编辑:

<service
android:name=".MyWallpaperService"
android:enabled="true"
android:label="Wallpaper Example"
android:permission="android.permission.BIND_WALLPAPER">

<intent-filter>
<action android:name="android.intent.action.WallpaperService"/>
</intent-filter>

<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/mywallpaper">

</meta-data>

</service>

最佳答案

确保您的服务已在 list 中声明并且具有适当的权限:

The service requires the permission android.permission.BIND_WALLPAPER and must be registered via an intent-filter for the android.service.wallpaper.WallpaperService action.

此外,不要忘记在您的 list 中声明它

<service android:enabled="true" android:name=".MyWallPaperservice" />

关于android - Activity 不调用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28266827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com