从最近的应用程序中删除应用程序时自定义静态广播接收器无法正常工作

时间:2017-02-10 07:07:50

标签: android broadcastreceiver

当我的应用程序从最近的应用程序中删除时,我的自定义广播接收器无法正常工作,不能再接受onreceive,但我们知道当应用程序从最近的应用程序中删除时可以收到系统广播。这种现象是对的吗?或者我的定制广播接收器不是真的吗?谢谢你的帮助。

<receiver android:name=".AlarmBroadcastReceiver"
    android:exported="true">
    <intent-filter>
       <action android:name="action_alarm_trigger_sync" />
          <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
 </receiver>

Intent serviceIntent = new Intent();
serviceIntent.setAction(AlarmBroadcastReceiver.ACTION);
serviceIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent servicePendingIntent =
PendingIntent.getBroadcast(context,
       AlarmBroadcastReceiver.SERVICE_ID, serviceIntent,
       PendingIntent.FLAG_CANCEL_CURRENT);  
am.setRepeating(
       AlarmManager.RTC_WAKEUP, 
       cal.getTimeInMillis(),
       interval,
       servicePendingIntent
);

BroadcastReceiver代码(粘贴自评论):

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("AlarmManager","AlarmBroadcastReceiver start");
    try {
        FileUtils.writeFileSdcardFile("hhaa"+ System.currentTimeMillis()+"\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
} 


public static void writeFileSdcardFile(String write_str) throws IOException {
    File mFile = new File("/mnt/sdcard/alram_record2.text");
    if(!mFile.exists()){
        mFile.createNewFile();
    }
    FileOutputStream fout = new FileOutputStream(mFile, true);
    byte [] bytes = write_str.getBytes();
    fout.write(bytes);
    fout.close();
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<receiver android:name=".AlarmBroadcastReceiver" />

Intent serviceIntent = new Intent(this, AlarmBroadcastReceiver.class);

PendingIntent.getBroadcast(context, AlarmBroadcastReceiver.SERVICE_ID, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, servicePendingIntent);