当action.BOOT_COMPLETED时,BroadCastReceiver不会被调用

时间:2017-02-07 23:45:23

标签: android android-broadcastreceiver

编辑2:>开始
可能是在我的清单中,接收器丢失了

                  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

编辑2:>结束
EDIT1:>开始我不能使用默认的android工作室模拟器,我已经将模拟器连接到工作室。我注意到当设备重启时我得到了

  

W / ActivityManager:无法启动服务Intent {cmp = com.google.android.gms / .clearcut.init.ClearcutBootCompleteIntentService} U = 0:未找到

是否与之相关?
EDIT1产品:>结束
我有两个接收器,一个在启动完成时被调用。第二个应该从第一个接收一个intent并调用intentService。事情是,它不起作用

我的一个清单

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <service
        android:name=".getStalkersOnDemand_Service"
        android:exported="false" />
        <receiver
        android:name=".OnBootStartGetReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
    <receiver android:name=".getReceiver"/>

第一个接收器

public class OnBootStartGetReceiver extends BroadcastReceiver {
public OnBootStartGetReceiver() {
}

//Intent-filter BOOT_COMPLETE added to this one , making sure that it fires off when the device has booted
@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);          //create the alarm manager
    Intent i = new Intent(context, getReceiver.class);                                       //Create a new intent sending and the other receiver
    PendingIntent pI = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);      //Create a pending intent {context,singal(we define it),intent,Flag}
    Calendar rightNow = Calendar.getInstance();                                                         //get the instance of the calendar to a local calendar
    rightNow.add(Calendar.MINUTE, 10);                                                                   //add to it ,10 minutes
    alarmManager.setInexactRepeating(AlarmManager.RTC, rightNow.getTimeInMillis(), 30 * 1000, pI);        //Resend the singal {don't wake up the device,starting time,interval,pending inent}
}}

第二个

public class getReceiver extends BroadcastReceiver {
public getReceiver() {
}
//This receiver gets fired off by the OnBootStartGetReceiver
@Override
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent(context, getStalkersOnDemand_Service.class);              //create the intent giving {context ,the class to start}
    context.startService(serviceIntent);                                                        //start the service
}}

最后一段intentService

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {                              //When we start the service/thread
    Toast.makeText(this, "Getting other users location...", Toast.LENGTH_LONG).show();          //Inform the user
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {                                                                       //When the user stops the service
    super.onDestroy();
    Toast.makeText(this, "Stalker ended...", Toast.LENGTH_LONG).show();                       //He gets informed
}

protected void onHandleIntent(Intent intent) {
    System.out.println("I run");
}

我对Android很新,但我无法理解为什么它不起作用。

0 个答案:

没有答案