Activity 未调用服务
Activity not calling service
我是 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);
}
}
我的服务 class 看起来像这样
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>
确保您的服务已在清单中声明并且具有适当的权限:
The service requires the permission android.permission.BIND_WALLPAPER
and must be registered via an intent-filter for the
android.service.wallpaper.WallpaperService action.
此外,不要忘记在您的清单中声明它
<service android:enabled="true" android:name=".MyWallPaperservice" />
好吧,这很愚蠢。我把部分放在清单文件中的区域之外。现在感觉好傻
我是 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);
}
}
我的服务 class 看起来像这样
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>
确保您的服务已在清单中声明并且具有适当的权限:
The service requires the permission android.permission.BIND_WALLPAPER and must be registered via an intent-filter for the android.service.wallpaper.WallpaperService action.
此外,不要忘记在您的清单中声明它
<service android:enabled="true" android:name=".MyWallPaperservice" />
好吧,这很愚蠢。我把部分放在清单文件中的区域之外。现在感觉好傻