我是Android API的新手,我正在尝试实施一个报告回应用程序的计划流程。
所以,我在MainActivity
中有以下代码:
public class MainActivity extends AppCompatActivity {
private AlarmManager almgr;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter statusIntentFilter = new IntentFilter("Report");
BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Receiver","Received!");
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(
receiver, statusIntentFilter);
Intent mServiceIntent = new Intent(MainActivity.this, Background.class);
almgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getService(this,0,mServiceIntent,0);
almgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+1000, alarmIntent);
}
}
这是我的IntentService class
public class Background extends IntentService {
public Background(){super("Background");}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(this.toString(),"Service called");
Intent localIntent = new Intent("Report")
.putExtra("Value","True");
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
IntentService
正在接收初始Intent
,并且打印了Background类中的Log条目,但BroadcastReceiver未收到Report Intent
。它不打印" Receiver Received"日志。