未找到处理 Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER 的活动(有附加功能)

Posted

技术标签:

【中文标题】未找到处理 Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER 的活动(有附加功能)【英文标题】:No Activity found to handle Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras) 未找到处理 Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER 的活动(有附加功能) 【发布时间】:2013-02-03 02:21:57 【问题描述】:

我正在为 android 创建一个动态壁纸应用程序。我已经写了一些代码,但是我收到了以下错误原因:android.content.ActivityNotFoundException: No Activity found to handle Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras)

请帮忙

MyPoint.java

public class MyPoint 
public String text;
public float x;
public float y;

public MyPoint(String text, float x, float y) 
    this.text = text;
    this.x = x;
    this.y = y;



MyPreferencesActivity.java

public class MyPreferencesActivity extends PreferenceActivity 
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

    Preference circlePreference = getPreferenceScreen().findPreference(
            "numberOfCircles");
    circlePreference.setOnPreferenceChangeListener(numberCheckListener);


Preference.OnPreferenceChangeListener numberCheckListener = new OnPreferenceChangeListener() 

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) 
        if (newValue != null && newValue.toString().length() > 0
                && newValue.toString().matches("\\d*")) 
            return true;
        
        Toast.makeText(MyPreferencesActivity.this, "Invalid Input",
                Toast.LENGTH_SHORT).show();
        return false;
    
;

MyWallpaperService.java

public class MyWallpaperService extends WallpaperService 

@Override
public Engine onCreateEngine() 
    return new MyWallpaperEngine();


private class MyWallpaperEngine extends Engine 
    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() 
        @Override
        public void run() 
            draw();
        

    ;
    private List<MyPoint> circles;
    private Paint paint = new Paint();
    private int width;
    int height;
    private boolean visible = true;
    private int maxNumber;
    private boolean touchEnabled;

    public MyWallpaperEngine() 
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(MyWallpaperService.this);
        maxNumber = Integer
                .valueOf(prefs.getString("numberOfCircles", "4"));
        touchEnabled = prefs.getBoolean("touch", false);
        circles = new ArrayList<MyPoint>();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(10f);
        handler.post(drawRunner);
    

    @Override
    public void onVisibilityChanged(boolean visible) 
        this.visible = visible;
        if (visible) 
            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);
    

    @Override
    public void onTouchEvent(MotionEvent event) 
        if (touchEnabled) 

            float x = event.getX();
            float y = event.getY();
            SurfaceHolder holder = getSurfaceHolder();
            Canvas canvas = null;
            try 
                canvas = holder.lockCanvas();
                if (canvas != null) 
                    canvas.drawColor(Color.BLACK);
                    circles.clear();
                    circles.add(new MyPoint(
                            String.valueOf(circles.size() + 1), x, y));
                    drawCircles(canvas, circles);

                
             finally 
                if (canvas != null)
                    holder.unlockCanvasAndPost(canvas);
            
            super.onTouchEvent(event);
        
    

    private void draw() 
        SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try 
            canvas = holder.lockCanvas();
            if (canvas != null) 
                if (circles.size() >= maxNumber) 
                    circles.clear();
                
                int x = (int) (width * Math.random());
                int y = (int) (height * Math.random());
                circles.add(new MyPoint(String.valueOf(circles.size() + 1),
                        x, y));
                drawCircles(canvas, circles);
            
         finally 
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        
        handler.removeCallbacks(drawRunner);
        if (visible) 
            handler.postDelayed(drawRunner, 5000);
        
    

    // Surface view requires that all elements are drawn completely
    private void drawCircles(Canvas canvas, List<MyPoint> circles) 
        canvas.drawColor(Color.BLACK);
        for (MyPoint point : circles) 
            canvas.drawCircle(point.x, point.y, 20.0f, paint);
        
    


SetWallpaperActivity.java

public class SetWallpaperActivity extends Activity 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_wallpaper);


public void clickMe(View view) 
    Intent intent = new Intent(
            WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
    intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
            new ComponentName(this, MyWallpaperService.class));
    startActivity(intent);



activity_set_wallpaper.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".SetWallpaperActivity" >

<Button 
    android:layout_
    android:layout_
    android:onClick="clickMe"
    android:text="Click Me!" />

</RelativeLayout>

mywallpaper.xml

<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/wallpaper_description"
android:settingsActivity="de.vogella.android.wallpaper.MyPreferencesActivity"
android:thumbnail="@drawable/icon" />

prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<CheckBoxPreference
    android:key="touch"
    android:title="Enable Touch" >
</CheckBoxPreference>

<EditTextPreference
    android:key="numberOfCircles"
    android:title="Number of Circles" >
</EditTextPreference>

</PreferenceScreen>

记录猫

02-18 21:08:07.630: E/AndroidRuntime(9406): FATAL EXCEPTION: main
02-18 21:08:07.630: E/AndroidRuntime(9406): java.lang.IllegalStateException: Could not execute method of the activity
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.view.View$1.onClick(View.java:3095)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.view.View.performClick(View.java:3567)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.view.View$PerformClick.run(View.java:14224)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.os.Handler.handleCallback(Handler.java:605)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.os.Looper.loop(Looper.java:137)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.app.ActivityThread.main(ActivityThread.java:4517)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at dalvik.system.NativeStart.main(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406): Caused by: java.lang.reflect.InvocationTargetException
02-18 21:08:07.630: E/AndroidRuntime(9406):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.view.View$1.onClick(View.java:3090)
02-18 21:08:07.630: E/AndroidRuntime(9406):     ... 11 more
02-18 21:08:07.630: E/AndroidRuntime(9406): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent  act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras) 
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1536)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1388)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.app.Activity.startActivityForResult(Activity.java:3195)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at android.app.Activity.startActivity(Activity.java:3302)
02-18 21:08:07.630: E/AndroidRuntime(9406):     at de.vogella.android.wallpaper.SetWallpaperActivity.clickMe(SetWallpaperActivity.java:23)
02-18 21:08:07.630: E/AndroidRuntime(9406):     ... 14 more

【问题讨论】:

见 [***.com/questions/14317167/…. [1]:***.com/questions/14317167/… 【参考方案1】:

我不知道您要构建的目标是什么,但the documentation 说WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER 是在 API 级别 16 中添加的,因此如果您在旧设备上使用此代码,它将引发此错误。 This answer 展示了如何检查正确的 API 级别。

除了检查 API 级别,您还可以将意图包装在 try/catch 中。虽然这通常是不好的做法,但我认为这在这里可能是有益的,因为有些人偶尔会在 API 级别大于 16 时报告此错误(尽管这种情况很少见,并且可能与用户不使用库存 android ROM 有关)。

【讨论】:

在 api 级别 23 中存在同样的问题

以上是关于未找到处理 Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER 的活动(有附加功能)的主要内容,如果未能解决你的问题,请参考以下文章

android.content.ActivityNotFoundException:未找到处理 Intent act=android.settings.USAGE_ACCESS_SETTINGS

未找到处理 Intent act=com.android.camera.action.CROP 的 Activity

未找到处理 Intent act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER 的活动(有附加功能)

未找到处理 Intent 的活动 - android.intent.action.OPEN_DOCUMENT

未找到Android活动异常:找不到处理Intent的活动

getActivity() 调用导致 RuntimeException:无法启动意图 Intent act=android.intent.action.MAIN