在我的 Android 应用程序中,我尝试使用
BroadcastReceiver
来收听传入的短信。
但由于某种原因,
onReceive
方法从未被调用,而且似乎连构造函数也根本没有运行。
为了测试接收器,我尝试使用
"android.intent.action.AIRPLANE_MODE"
意图过滤器,因为它似乎是最简单的触发方式。
这是我的 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CallsListener"
tools:targetApi="31" >
<receiver android:name=".MessagesReceiver"
android:exported="false">
<intent-filter android:priority="999">
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是接收者类:
package com.sagiziv.callslistener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import java.util.Date;
public class MessagesReceiver extends BroadcastReceiver {
public MessagesReceiver() {
super();
Log.d("pttt", "Receiver created");
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("pttt", "onReceive: " + intent.getAction());
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
if (extras != null) {
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
// Handle incoming SMS
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
}
}
}
}
没有打印任何日志,我不明白为什么。
我在运行 Android 8.0.0 版本的手机和运行 Android 13 版本的平板电脑上进行了测试。
===编辑===
我通过在活动中注册接收器来使其工作,如下所示:
package com.sagiziv.callslistener;
import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 123;
private MessagesReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
}
private void requestPermissions() {
if (checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.READ_SMS,
Manifest.permission.READ_PHONE_STATE
}, PERMISSION_REQUEST_CODE);
} else {
myReceiver = new MessagesReceiver();
// Register MyReceiver for SMS
registerReceiver(myReceiver, new IntentFilter("android.intent.action.AIRPLANE_MODE"));
registerReceiver(myReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}
}
@Override
protected void onDestroy() {
// Unregister MyReceiver when the activity is destroyed
if (myReceiver != null) {
unregisterReceiver(myReceiver);
Log.d("pttt", "Receiver unregistered");
}
super.onDestroy();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
// Check if permissions are granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Denied", Toast.LENGTH_LONG).show();
}
}
}
}
这适用于飞行模式,但不适用于短信。
我通过向自己发送一条短信来测试短信,这可能是问题所在吗?
在我的手机上,短信发送会延迟 6 秒(这是消息应用程序中的一项功能),因此我可以在消息发送并立即收到之前返回到我的应用程序。
你需要使用:
<receiver android:name=".IncomingSms"
android:exported="false">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
而不是:
<receiver android:name=".MessagesReceiver"
android:exported="false">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
问题在于操作名称