我已经搜索了几个小时,发现了类似的问题并尝试了这些解决方案,但到目前为止还没有任何工作。我正在使用Android工作室与运行Android版本23的模拟器。我怀疑我的问题是在某个地方有SMS_RECEIVE的权限,因为我的其他操作正在触发我的接收器的行为。这是我的清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsMonitor" android:exported="true" android:permission="android.permission.BROADCAST_SMS" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="myAction.updateKeyword" />
<action android:name="myAction.closeApp" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
这是我的接收器的代码:
package com.example.the_f.smsalert;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.NotificationCompat;
import android.telephony.SmsMessage;
import static android.R.drawable.stat_notify_more;
/**
* Created by the_f on 11/10/2016.
*/
public class SmsMonitor extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
private final String UPDATE_KEYWORD = "myAction.updateKeyword";
private final String CLOSE_APP = "myAction.closeApp";
private final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
System.out.println("action is: " + action);
//only want to inspect the message if the app is active
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
System.out.println("the action was sms received");
Bundle extras = intent.getExtras();
if(extras != null){
Object[] messages = (Object[]) extras.get(SMS_BUNDLE);
String messageContent = "";
for(int i = 0; i < messages.length; i++){
SmsMessage msg = SmsMessage.createFromPdu((byte[]) messages[i]);
messageContent = msg.getMessageBody().toString();
}
System.out.println("build sms message: " + messageContent);
inspectForKeyword(messageContent.toLowerCase());
}
//update the keyword for the app. also sets the receiver to active to start monitoring
}else if(action.equals(UPDATE_KEYWORD)){
keyword = intent.getStringExtra("keyword").toLowerCase();
active = true;
System.out.println("changing keyword to: " + keyword);
testAlarm();
//the main activity was closed and so this receiver should stop monitoring
}else if(action.equals(CLOSE_APP)){
System.out.println("setting smsMonitor in inactive");
active = false;
}else{
//not a valid intent for this receiver. shouldnt get here.
}
}
private void inspectForKeyword(String messageContent) {
if (messageContent.contains(keyword)) {
//activate vibrate
//activate ringer
//activate led flash
//send notification
}
}
private void testAlarm(){
//binds an activity to start when the notification is clicked
Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.setType("android.intent.category.APP_MESSAGING");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , smsIntent, 0);
//builds the notification
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context);
notifBuilder.setSmallIcon(stat_notify_more);
notifBuilder.setContentTitle("You have an Emergency Alert!");
notifBuilder.setContentText("Check your messages for an Emergency!");
notifBuilder.setContentIntent(pendingIntent);
//sends notification
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(0, notifBuilder.build());
Vibrator vibes = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibes.vibrate(3000);
}
}
大多数java代码都可以忽略。我有一堆我试图测试的代码。但是,在onReceive()的顶部,我有一个print语句来打印操作的名称。当我使用其他两个动作时,print语句会触发,但是当我使用模拟器并模拟接收文本时,则不会触发。模拟器的消息传递应用程序指示它在我模拟发送短信时收到消息,但我的广播接收器没有得到通知。我真的可以使用一些帮助。我对我的问题一无所知。