我的Android進階之旅------>怎樣解決Android 5.0中出現的警告: Service Intent must be explicit:


我的Android進階之旅——>怎樣解決Android 5.0中出現的警告: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xtc.kuwo.watch.MUSIC_PLAY_SERVICE (has extras) }

1.錯誤描寫敘述

今天在Android4.4 的小米4手機上執行我的程序的時候沒有報錯。而在Android 5.1的華為P7上執行我的程序的時候報了以下的錯誤,錯誤提演示樣例如以下:

E/AndroidRuntime(12500): FATAL EXCEPTION: main
E/AndroidRuntime(12500): Process: com.xtc.watch, PID: 12500
E/AndroidRuntime(12500): java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.xtc.kuwo.watch.MUSIC_PLAY_SERVICE (has extras) }
E/AndroidRuntime(12500):        at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1847)
E/AndroidRuntime(12500):        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1876)
E/AndroidRuntime(12500):        at android.app.ContextImpl.startService(ContextImpl.java:1860)
E/AndroidRuntime(12500):        at android.content.ContextWrapper.startService(ContextWrapper.java:516)
E/AndroidRuntime(12500):        at com.xtc.watch.kuwo.activity.WatchMusicPlay.pauseMusic(WatchMusicPlay.java:314)
E/AndroidRuntime(12500):        at com.xtc.watch.kuwo.activity.WatchMusicPlay.access$600(WatchMusicPlay.java:32)
E/AndroidRuntime(12500):        at com.xtc.watch.kuwo.activity.WatchMusicPlay$3.onClick(WatchMusicPlay.java:220)
E/AndroidRuntime(12500):        at android.view.View.performClick(View.java:4790)
E/AndroidRuntime(12500):        at android.view.View$PerformClick.run(View.java:19933)
E/AndroidRuntime(12500):        at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(12500):        at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(12500):        at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(12500):        at android.app.ActivityThread.main(ActivityThread.java:5569)
E/AndroidRuntime(12500):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12500):        at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(12500):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime(12500):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)

這里寫圖片描寫敘述

而我啟動Service的Intent代碼例如以下所看到的:

        Intent intent = new Intent();
        intent.setAction(MUSIC_PLAY_SERVICE);
        intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
        intent.putExtra("musicURL", musicURL); //歌曲URL
        startService(intent);

2.錯誤原因

有些時候我們使用Service的時須要採用隱私啟動的方式,可是Android 5.0一出來后。當中有個特性就是Service Intent must be explitict,也就是說從Android Lollipop版本號(Android 5.0)開始。service服務必須採用顯示方式啟動。

而android源代碼是這樣寫的(源代碼位置:sdk/sources/android-21/android/app/ContextImpl.java):

startService(Intent service)方法

startService(Intent service)方法代碼例如以下

 @Override
    public ComponentName startService(Intent service) {
        warnIfCallingFromSystemProcess();
        return startServiceCommon(service, mUser);
    }

startServiceCommon(Intent service, UserHandle user)方法

上面的startService(Intent service)方法調用的是startServiceCommon(Intent service, UserHandle user)。代碼例如以下所看到的:

 private ComponentName startServiceCommon(Intent service, UserHandle user) {
        try {
            validateServiceIntent(service);
            service.prepareToLeaveProcess();
            ComponentName cn = ActivityManagerNative.getDefault().startService(
                mMainThread.getApplicationThread(), service,
                service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
            if (cn != null) {
                if (cn.getPackageName().equals("!")) {
                    throw new SecurityException(
                            "Not allowed to start service " + service
                            + " without permission " + cn.getClassName());
                } else if (cn.getPackageName().equals("!!")) {
                    throw new SecurityException(
                            "Unable to start service " + service
                            + ": " + cn.getClassName());
                }
            }
            return cn;
        } catch (RemoteException e) {
            return null;
        }
    }

validateServiceIntent(Intent service)方法

上面的startServiceCommon(Intent service, UserHandle user)方法中調用的validateServiceIntent(Intent service)方法代碼例如以下所看到的:

 private void validateServiceIntent(Intent service) {
        if (service.getComponent() == null && service.getPackage() == null) {
            if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
                IllegalArgumentException ex = new IllegalArgumentException(
                        "Service Intent must be explicit: " + service);
                throw ex;
            } else {
                Log.w(TAG, "Implicit intents with startService are not safe: " + service
                        + " " + Debug.getCallers(2, 3));
            }
        }
    }

能夠看得出來,就是在validateServiceIntent(Intent service)方法中推斷假設大於Build.VERSION_CODES.LOLLIPOP版本號的話。而且啟動Service的Intent假設沒有設置Component和Package的話就會跑出異常java.lang.IllegalArgumentException: Service Intent must be explicit:

版權聲明:本文為【歐陽鵬】原創文章。歡迎轉載,轉載請注明出處!
http://blog.csdn.net/ouyang_peng/article/details/50727693

3.解決方法

設置要啟動Service的Intent的Action和packageName

        Intent intent = new Intent();
        intent.setAction(MUSIC_PLAY_SERVICE);
        intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
        intent.putExtra("musicURL", musicURL); //歌曲URL
        startService(intent);

這里寫圖片描寫敘述

改為:

        Intent intent = new Intent();
        intent.setAction(MUSIC_PLAY_SERVICE);
        //不加這句話的話 android 5.0以上會報:Service Intent must be explitict
        intent.setPackage(getPackageName());
        intent.putExtra("MSG", Constants.PlayerMsg.PAUSE_MSG); //暫停播放音樂
        intent.putExtra("musicURL", musicURL); //歌曲URL
        startService(intent);

這里寫圖片描寫敘述

以上代碼就是加了一行

        //不加這句話的話 android 5.0以上會報:Service Intent must be explitict
        intent.setPackage(getPackageName());

此方式是google官方推薦使用的解決方法。

在此附上地址供大家參考:http://developer.android.com/goo … tml#billing-service,有興趣的能夠去看看。

以下是http://developer.android.com/goo … tml#billing-service站點的截圖。例如以下所看到的:

google官方站點上的提示

版權聲明:本文為【歐陽鵬】原創文章,歡迎轉載,轉載請注明出處!
http://blog.csdn.net/ouyang_peng/article/details/50727693

作者:歐陽鵬 歡迎轉載,與人分享是進步的源泉。
轉載請保留原文地址:http://blog.csdn.net/ouyang_peng

這里寫圖片描寫敘述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM