I have a blank HelloWorld Application:
package tutorials.TestReceivers;
import android.app.Activity;
import android.os.Bundle;
public class TestReceiversActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
With this BootReceiver.Java:
package tutorials.TestReceivers;
import android.content.BroadcastReceiver;
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, TestReceiversActivity.class);
context.startActivity(intent);
}
}
and this manifest:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestReceiversActivity"
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-permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="development.TestReceiversActivity.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
</application>
</manifest>
After running the application and closing it.
When I unlock screen (SCREEN_ON) nothing happend.
And when i boot the decive I'm getting next msg like:
"The application TestReceiversActivity (tutorials.TestReceivers process) stop unexpectedly. Try again"
After a long time of frustration, I solved the problem above.
The right way to register Boot Broadcast Receiver (and open activity according to it), is:
Blank HelloWorld Application (TestReceiversActivity.java):
package tutorials.TestReceivers;
import android.app.Activity;
public class TestReceiversActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Another Boot Receiver Class (BootReceiver.java)
package tutorials.TestReceivers;
import android.content.BroadcastReceiver;
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("DAVID", "Hi, Boot reciver was catch!");
Intent i = new Intent(context, TestReceiversActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Note: You must set the flag to make it work!
Set the manifest to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tutorials.TestReceivers"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".TestReceiversActivity"
android:label="@string/app_name">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Enjoy!
Delete android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
. Add a <uses-permission>
element for this permission as a child of the <manifest>
element.
If the problems continue, use adb logcat
, DDMS, or the DDMS perspective in Eclipse to look at LogCat and examine the stack trace associated with your crash.
Here is a sample project showing how to get control at boot time.
SCREEN_ON
will not work from the manifest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With