本文介绍了每当新短信到达时启动广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个远程服务,该服务正在调用广播接收器,该广播接收器又调用警报管理器以触发警报,但是我想在有新消息到达时通过rempte服务来触发接收器
I have created a remote service which , is calling a broadcast receiver which in turn call the alarm manager for triggering the alarm , but i want to trigger the receiver through the rempte service when ever a new message arrives
代码段如下:
@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
}
}
};
推荐答案
如果您的服务正在运行,则您的代码正在运行。 。因此,只要您收到SMS,就会运行以下代码。您可以像获取短信后修改此内容,然后开始服务并将代码放入服务中。
Your code is working if your service is running... so below code is run whenever you get SMS. you can modify this like after get SMS you can start service and put your code in to servies.
SmsReceiver JAVA类
SmsReceiver JAVA Class
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null)
return;
// To display a Toast whenever there is an SMS.
Toast.makeText(context,"Recieved",Toast.LENGTH_LONG).show();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
Toast.makeText(context, ""+sender+":"+body , Toast.LENGTH_SHORT)
.show();
}
startAlarmNotification();
}
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);
}
}
在清单中执行以下代码:
in your manifest do below code :
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
还添加以下权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
这篇关于每当新短信到达时启动广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!