Android - 无法在设备启动时自动启动活动?
我正在尝试在设备启动时启动一项活动。
奇怪的是,我的代码可以在模拟器上运行,但不能在实际设备上运行。
我有一个名为preferences.xml 的文件,其中有一个CheckBoxPreference 用于设置自动启动的打开和关闭。
这是我的代码:-
public class AutoStartUp extends BroadcastReceiver
{
private static final String TAG = "AutoStartUp";
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED))
{
SharedPreferences sp = context.getSharedPreferences("preferences", 0);
if(sp.getBoolean("autostartup", false))
{
Log.e(TAG, "I AM HERE");
Intent startupIntent = new Intent(context, CompleteTaskManager.class);
context.startActivity(startupIntent);
}
}
}
}
这是我的此接收器的清单部分:-
<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
请帮助!
I am trying to start an activity at device bootup.
Stranege thing is that my code is working on emulator but not on an actual device.
I have a file called preferences.xml which has a CheckBoxPreference to set the auto start on and off.
Here is my code :-
public class AutoStartUp extends BroadcastReceiver
{
private static final String TAG = "AutoStartUp";
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED))
{
SharedPreferences sp = context.getSharedPreferences("preferences", 0);
if(sp.getBoolean("autostartup", false))
{
Log.e(TAG, "I AM HERE");
Intent startupIntent = new Intent(context, CompleteTaskManager.class);
context.startActivity(startupIntent);
}
}
}
}
Here is my Manifest part for this receiver :-
<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Please Help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试此 FLAG_ACTIVITY_REORDER_TO_FRONT
http://developer.android.com/reference/android/content/Intent .html#FLAG_ACTIVITY_REORDER_TO_FRONT
You can try this FLAG_ACTIVITY_REORDER_TO_FRONT
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT