重新启动后以广播接收器为主的Android应用程序
您好,我想开发一个简单的应用程序,没有主要活动作为启动器 我想注册一个广播接收器,它在设备重新启动后启动,在OnReceive回调中启动一个活动 这是我的舱单重新启动后以广播接收器为主的Android应用程序,android,broadcastreceiver,Android,Broadcastreceiver,您好,我想开发一个简单的应用程序,没有主要活动作为启动器 我想注册一个广播接收器,它在设备重新启动后启动,在OnReceive回调中启动一个活动 这是我的舱单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.examples" android:versionCode
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
最后是主要活动
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我的代码有什么问题
提前谢谢
弗朗西斯科我的代码正在工作..这是它 明显地
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
我看不出你的代码有什么问题,但是我有一些东西值得一试 将权限移出
应用程序
标记:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
还是不工作?尝试增加一些延迟,如前所述:
引自上述链接:
然而,我建议延迟几秒,例如,10秒,然后
运行(1)条线路,对于不同的手机和
服务
例如,在我的例子中,我的服务将写入sd卡。如果你
立即启动您的服务,有些手机可能会因为sd而出现故障
卡还没准备好
从android 3.1开始,如果应用程序服务管理器没有处于活动状态的上下文(即,至少有一个活动或服务将进程保持在“活动状态”),那么它就不能生成广播接收器 规范摘录
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
您需要以某种方式启动应用程序,然后以休眠状态发送(但在应用程序管理器中注册)。您可以为此使用服务。强烈建议不要从BroadcastReceiver启动活动: 切勿启动活动以响应传入的广播意图 在我的例子中,PackageManager.DONT_KILL_应用程序帮助: 我已经试用过MIUI8固件真实设备小米Redmi Note 3 我的发现是:
- 您必须将应用程序添加到自动运行,以使其能够通过广播启动。我已经用Viber、WhatsApp等严肃的应用程序检查过了
- 我比较了清单设置(不以编程方式启用Receiver):
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//run your service
}
}, 10000);
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);