I have an application that you can completely and very easily recreate with the code that I will post on this issue. Here's the manifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcasttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcasttest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.broadcasttest.TestReceiver" android:label="@string/app_name" android:enabled="true" > </receiver> <intentservice android:name="com.example.broadcasttest.MonitorService" android:enabled="true" > <intent-filter> <action android:name="com.example.broadcasttest.MonitorService" /> </intent-filter> </intentservice> </application> </manifest>
As you can see, it contains an action, (wakefulness) of the broadcast receiver and intenservice, all in one package. The action starts when it starts, here is the code:
package com.example.broadcasttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendBroadcast(new Intent(this, TestReceiver.class)); } @Override public boolean onCreateOptionsMenu(Menu menu) {
This successfully runs the onReceive TestReceiver function.
package com.example.broadcasttest; import android.content.Context; import android.content.Intent; import android.support.v4.content.WakefulBroadcastReceiver; public class TestReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
In this case, everything goes wrong, although I set a breakpoint in the onReceive function, and it is definitely called. However, the MonitorService class MonitorService never reached. I put a breakpoint in the onHandleEvent function, but it looks like it never goes that far. Here is the code for this class:
package com.example.broadcasttest; import android.app.IntentService; import android.content.Intent; public class MonitorService extends IntentService { public MonitorService(String name) { super(name); } public MonitorService() { super("MonitorService"); } @Override protected void onHandleIntent(Intent intent) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { TestReceiver.completeWakefulIntent(intent); } } }
As you can tell from the commented line in the TestReceiver class, I tried using implicit intent instead of explicit. I also read this question and tried everything that was mentioned there. Am I missing something? I run this on an emulator (Nexus7 API L).
Is there anything that I am missing here?