我有(我確信這是一個簡單的)與棒棒糖和明確意圖的問題。我已經構建了一個應用程序,通過藍牙連接與打印機進行通信 - 這適用於低於棒棒糖的任何事情。Android棒棒糖 - 服務意圖必須明確
我已經檢查了各種其他答案和Android文檔,但看不到他們創建明確意圖的方式與我這樣做的方式之間的任何區別。
從logcat的錯誤是:
03-02 14:35:43.471: E/AndroidRuntime(28094): FATAL EXCEPTION: main
03-02 14:35:43.471: E/AndroidRuntime(28094): Process: com.example.app, PID: 28094
03-02 14:35:43.471: E/AndroidRuntime(28094): java.lang.RuntimeException: Error receiving broadcast Intent { act=printing flg=0x10 (has extras) } in [email protected]
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:969)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.os.Handler.handleCallback(Handler.java:739)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.os.Handler.dispatchMessage(Handler.java:95)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.os.Looper.loop(Looper.java:155)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.ActivityThread.main(ActivityThread.java:5696)
03-02 14:35:43.471: E/AndroidRuntime(28094): at java.lang.reflect.Method.invoke(Native Method)
03-02 14:35:43.471: E/AndroidRuntime(28094): at java.lang.reflect.Method.invoke(Method.java:372)
03-02 14:35:43.471: E/AndroidRuntime(28094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
03-02 14:35:43.471: E/AndroidRuntime(28094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
03-02 14:35:43.471: E/AndroidRuntime(28094): Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=printing flg=0x10 (has extras) }
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1786)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.ContextImpl.stopServiceCommon(ContextImpl.java:1844)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.ContextImpl.stopService(ContextImpl.java:1805)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.content.ContextWrapper.stopService(ContextWrapper.java:520)
03-02 14:35:43.471: E/AndroidRuntime(28094): at com.example.app.AppName$1.onReceive(ExampleClass.java:530)
03-02 14:35:43.471: E/AndroidRuntime(28094): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:950)
03-02 14:35:43.471: E/AndroidRuntime(28094):
... 8 more
我調用此函數從其他3個活動,已經把它在它自己的類並做稱它爲以下內容:
// In the activity I call it from
ExampleClass example = new Example(this);
example.print(); // Function in the example class
然後在此打印功能中:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(PRINT_ACTION);
ctx.registerReceiver(intentReciever, intentFilter); // Where ctx is the context
Intent i = new Intent(ctx, printerService.class);
i.putExtras(bundle);
ctx.startService(i); // Start the relevant service
android文檔:
意圖downloadIntent = new Intent(this,DownloadService.class); downloadIntent.setData(Uri.parse(fileUrl)); startService(downloadIntent);
除了setData
和putExtras
,我看不出有什麼區別。
而在同一類以上(廣播接收器):
private BroadcastReceiver intentReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(intentReciever);
context.stopService(intent); // If I remove this, I get multiple returns, but if I leave it - it crashes :(
}
};
在printerClass.class(intentService)文件,我有:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ExampleClass.PRINT_ACTION);
broadcastIntent.putExtras(ReceivedBundle); // Put all the received data back in and send back!
getBaseContext().sendBroadcast(broadcastIntent);
對我來說,這看起來像該服務已經明確,但顯然不是!
這個問題看起來是在回報點,但我失去了它的虛幻!任何與此有關的幫助將被大力讚賞。
您需要在'stopService()' – Apurva 2015-03-02 15:33:27
@Apurva中傳遞'context',謝謝 - 但是如何做到這一點? 'context.stopService(intent);'傳遞上下文嗎? – theGuyWithCows 2015-03-02 15:41:18
[Android L(API 21) - java.lang.IllegalArgumentException:Service Intent必須顯式可能重複](https://stackoverflow.com/questions/27183164/android-l-api-21-java-lang-illegalargumentexception -service-intent-must-be) – rds 2017-10-16 13:02:09