我创建了一个远程服务,它正在调用一个广播接收器,该接收器又调用警报管理器来触发警报,但是我想在有新消息到达时通过 rempte 服务触发接收器
这段代码如下:
@Override
public IBinder onBind(Intent arg0) {
Log.d(getClass().getSimpleName(), "onBind()");
return myRemoteServiceStub;
}
private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {
public int getCounter() throws RemoteException {
// startAlarmNotification();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction("your_action_strings"); //further more
filter.addAction("your_action_strings"); //further more
registerReceiver(receiver, filter);
return counter;
}
};
@Override
public void onCreate() {
super.onCreate();
Log.d(getClass().getSimpleName(),"onCreate()");
}
@Override
public void onDestroy() {
super.onDestroy();
serviceHandler.removeCallbacks(myTask);
serviceHandler = null;
Log.d(getClass().getSimpleName(),"onDestroy()");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
serviceHandler = new Handler();
serviceHandler.postDelayed(myTask, 1000L);
Log.d(getClass().getSimpleName(), "onStart()");
}
class Task implements Runnable {
public void run() {
++counter;
serviceHandler.postDelayed(this,1000L);
Log.i(getClass().getSimpleName(), "Incrementing counter in the run method");
}
}
public void startAlarmNotification(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
Intent alarmintent = new Intent(RemoteService.this, AlarmReciver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, notificationId, alarmintent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60 * 1000, sender);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
startAlarmNotification();
unregisterReceiver(receiver);
}
else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
//action for phone state changed
}
}
};