着信SMSを聞くアプリを書いています。私は多くの質問と回答をオンラインでチェックしましたが、それでも動作しません。マイコード:SmsBroadcast受信機が動作しない
私の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を使用するSamsung s7とSony Xperia Z5 Compactと、Android 6.0.1を使用するSamsung s6の両方があります。これらのデバイスにメッセージを送信すると、デフォルトのメッセンジャーアプリだけが通知を表示します。
他の人の回答と質問を確認しました。デフォルトメッセンジャーアプリの優先度がintentFilter
より高いか、abortBroadcast()
と表示されているようです。私はまた、私の意図の優先順位をAndroidのドキュメントに定義されているように最大の整数または999に設定しましたが、まだ運がありません。
受信機はまだ登録されていますか? –
はい。 AndroidManifestに登録します。 Xml –
私はまた、受信者をプログラムで登録しようとしましたが、まだ運がありません。 –