尝试在我的应用中监控传入的短信。在Android Side,他们对来自 API 19 的监控传入短信做了一些更改。
无法在 API 23 中跟踪背景中的传入消息(在应用程序不在后台和移动屏幕处于关闭状态时)。
在Android文档中,如果您的应用不是 API 19 的默认消息应用,则无法在后台跟踪传入的短信。
参考: - Getting Your SMS Apps Ready for KitKat
但它正在使用我的 API 21(LOLLIPOP)设备。它不适用于 API 23(Marshmallow)设备。设备屏幕启用或应用程序处于打开状态时表示它在 API 23(Marshmallow)中工作。
您能否更新我有没有办法在 API 23(Marshmallow)中跟踪背景中的传入消息。
清单代码
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="5822">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
这里是我的Java代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
public static final String BROADCAST = "SMS_RECEIVER";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
Intent broadcast_intent;
String message_body = "", originAddress;
StringBuilder bodyText;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED)) {
broadcast_intent = new Intent(BROADCAST);
Bundle bundle = intent.getExtras();
SmsMessage[] messages;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
messages = new SmsMessage[pdus.length];
bodyText = new StringBuilder();
for (int i = 0; i < messages.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
} else {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
originAddress = messages[i].getOriginatingAddress().toUpperCase();
bodyText.append(messages[i].getMessageBody());
}
message_body = bodyText.toString();
}
}
}
}
答案 0 :(得分:2)
在介绍Android 6.0(API 23)之初,Google对应用权限技术进行了重大更改。因此,对于API&gt; = 23用户需要在应用运行时向应用授予权限,而不是在安装应用时
系统权限分为正常和危险两类:
访问手机短信管理器的权限被视为危险权限,这就是为什么需要在您尝试使用手机短信管理器的代码中以编程方式调用它。
您可以像这样
在mainActivity中申请权限int hasSMSPermission = checkSelfPermission( Manifest.permission.SEND_SMS );
List<String> permissions = new ArrayList<String>();
if( hasSMSPermission != PackageManager.PERMISSION_GRANTED ) {
permissions.add( Manifest.permission.SEND_SMS );
}
if( !permissions.isEmpty() ) {
requestPermissions( permissions.toArray( new String[permissions.size()] ), REQUEST_CODE_SOME_FEATURES_PERMISSIONS );
}
要接收许可的请求结果并设置你的brodcast接收器,请像这样制作onActivityResult的覆盖方法:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch ( requestCode ) {
case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
for( int i = 0; i < permissions.length; i++ ) {
if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
Log.d( "Permissions", "Permission Granted: " + permissions[i] );
} else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
Log.d( "Permissions", "Permission Denied: " + permissions[i] );
}
}
}
break;
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
请参阅此链接了解详情:https://developer.android.com/training/permissions/requesting.html
答案 1 :(得分:0)
基本上,您可以注册广播接收器以收听SMS_Receive
并查看以下内容。
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("mySMS");
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);
Log.i(TAG, "smsActivity : SMS is <" + sms.getMessageBody() +">");
String message = sms.getMessageBody();
//required sms
TextView tx = (TextView) findViewById(R.id.TextBox);
tx.setText(message);
} else
Log.i(TAG, "smsActivity : NULL SMS bundle");