设备重新启动接收器未在Android 2.2中调用

时间:2014-02-26 18:59:47

标签: android

我在android中实现设备重启接收器,我发现在我的情况下它没有执行,我试着看但是无法解决问题,如果可以帮助我

public class BootCompleteReceiver extends BroadcastReceiver implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener,
        LocationClient.OnAddGeofencesResultListener {

    static final int ONE_MINUTE = 60000;
    Context context;
    LocationClient locationClient;
    LocationRequest locationRequest;

    @Override
    public void onReceive(Context context, Intent arg1) {
        this.context = context;
        int resp = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(context);
        Toast.makeText(context, "Receiver Reboot", Toast.LENGTH_LONG).show();
    }
}

<receiver android:name="com.example.BootCompleteReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2 个答案:

答案 0 :(得分:0)

嗨,我认为你没有意图取决于尝试这个....

 @Override
public void onReceive(Context context, Intent intent) {
 this.context = context;
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);

        int resp = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(context);
        Toast.makeText(context, "Receiver Reboot", Toast.LENGTH_LONG).show();
    }

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

尝试一下,让我知道它是否有效

修改...

我为了实现你想要的东西而做的是这个...我是一个类,它唯一能做的就是为广播接收器做什么......当它得到广播接收器时创建一个意图,它就是这样的活动我想要发布......代码就是这个......

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent intent = new Intent(context, MyClass.class);
    context.startService(intent);
}
我的清单中的儿子看起来像这样......

<receiver android:name="com.example.app.StartMyServiceAtBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

enter image description here

答案 1 :(得分:0)

如果是针对更高版本(11+),则不会被调用可能是由于要求实际应用程序必须至少启动一次才能接收到android.intent.action.BOOT_COMPLETED事件。