我有一个空白的HelloWorld应用程序:
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);
}
}
使用此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);
}
}
并且这个清单:
<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>
运行应用程序并关闭它之后。
当我解锁屏幕(SCREEN_ON)时,没有任何事情发生。
当我启动决定时,我正在接下来的消息如下:
“应用程序TestReceiversActivity(tutorials.TestReceivers进程)意外停止。再试一次”
答案 0 :(得分:7)
注册引导广播接收器(以及根据它开放活动)的正确方法是:
空白的HelloWorld应用程序(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);
}
}
另一个Boot Receiver类(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);
}
}
注意:您必须设置标志才能使其正常工作!
将清单设置为:
<?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>
享受!
答案 1 :(得分:4)
删除android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
。为此权限添加<uses-permission>
元素作为<manifest>
元素的子元素。
如果问题仍然存在,请在Eclipse中使用adb logcat
,DDMS或DDMS透视图来查看LogCat并检查与崩溃关联的堆栈跟踪。
Here is a sample project显示如何在启动时获得控制权。
SCREEN_ON
无法在清单中使用。