我想写一个应用程序,它会在收到消息时执行某些操作。我有谷歌的答案很多天,并找到了一个像这样的解决方案:Link但它根本不适用于android 6或marshmallow。那么有没有办法唤醒我的应用程序,以便在android marshmallow上收到消息时做某事?
答案 0 :(得分:1)
以下MM的示例工作
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Logger.e("sender Num" + senderNum);
Logger.e("message" + message);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(broadcastReceiver, filter);
onDestroy方法取消注册广播接收器
unregisterReceiver(broadcastReceiver);