gpt4 book ai didi

java - 短信广播接收器不工作

转载 作者:行者123 更新时间:2023-12-02 12:52:34 25 4
gpt4 key购买 nike

我正在编写一个用于监听传入短信的应用程序。网上查了很多问题和答案,还是不行。我的代码:

我的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="superapp.networkapp">
<!--send and read SMS-->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity
android:name=".activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait">
</activity>

<receiver android:name=".utility.tool.SmsReceiver" >
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".activity.StartTestActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
tools:replace="screenOrientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

我的实用程序/工具/SmsReceiver.java

package superapp.networkapp.utility.tool;

import ...

public class SmsReceiver extends BroadcastReceiver {
//referenced from https://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("SMS", "onReceive: ");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] messages = null;
String sender;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
messages = new SmsMessage[pdus.length];
for(int i=0; i<messages.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]); //TODO
sender = messages[i].getOriginatingAddress();
String msgBody = messages[i].getMessageBody();
Toast.makeText(context,"Message: "+ msgBody + " from " + sender,Toast.LENGTH_LONG).show();
}
}
} catch(Exception e){
e.printStackTrace();
}
}
}
}
}

我有 askPermissions() 函数在运行时运行

   private static final List<String> permissions = Arrays.asList(
Manifest.permission.SEND_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS);
private void askPermissions() {
if (!hasAllPermissions())
ActivityCompat.requestPermissions(this, permissions.toArray(new String[0]), 69);
}
private boolean hasPermission(String permission) {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
}

private boolean hasAllPermissions() {
for (String permission : permissions) {
if (!hasPermission(permission)) return false;
}
return true;
}

我有 3 台设备:一台使用 Android 7.0 的三星 s7 和一台索尼 Xperia Z5 Compact,以及一台使用 Android 6.0.1 的三星 s6。当我向这些设备发送消息时,只有默认的消息应用程序会显示通知。

我检查了其他人的答案和问题。默认消息应用程序似乎具有更高的 intentFilter 优先级,或者它们调用 abortBroadcast()。我还将 Intent 的优先级设置为 Android 文档中定义的最大整数或 999,但还没有成功。

最佳答案

你可以试试这个,我也遇到了同样的问题,通过添加权限修复了。并且不需要以编程方式注册您的接收器,但您需要请求权限 Manifest.permission.READ_SMS

    <receiver
android:name=".name"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

关于java - 短信广播接收器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44561543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com