您好,我会开发一个简单的应用程序,而无需将主要活动作为启动器。
我想注册一个广播接收器,该接收器在设备重新启动后启动,并在OnReceive回调中启动活动
这是我的清单
<?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>
这是我的广播接收器
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
最后是主要活动
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>
我看不出你的代码有什么问题,但是我有一些值得尝试的东西。
将权限移出application
标记:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
如果不起作用,请简化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);
引用自上面的链接:
同时,我建议在 运行(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).
您需要以某种方式启动应用程序,然后以休眠状态(但在应用程序管理器中注册)发送它。您可以为此使用服务。
强烈建议不要从 BroadcastReciever 启动活动:https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
切勿启动活动以响应传入的广播意图。
就我而言,PackageManager.DONT_KILL_APP有所帮助:https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
我已经尝试了MIUI 8固件真实设备小米红米Note 3。
我的发现是:
您必须将应用程序添加到自动运行才能使其通过广播触发。我已经用Viber,WhatsApp等严肃的应用程序进行了检查。
我已经与清单设置进行了比较(没有以编程方式启用接收器):
<receiver android:name=".activities.broadcastrecievers.CallReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter>