class 接收短信无效
class for reciving sms not work
我使用此代码修改了阻止短信。但是当我将它安装在我的 phone 上并收到一条未在 logcat 中打印的短信日志时。我认为收到短信时 class 不是 运行。我在这里添加 BroadcastReceiver class 和 manifest.
public class SmsFilter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG","check if this class runs or not");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
Log.i("payam: ", "sms recived");
Log.i("payam: ", sender);
if (sender != null && Search.search(sender)==true) {
abortBroadcast();
}
return;
}
}
// ...
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="block.list"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FirstPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="@string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="@string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="@string/app_name" >
</activity>
</application>
您需要添加以下权限 -
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
这是一个工作教程Link
我使用此代码修改了阻止短信。但是当我将它安装在我的 phone 上并收到一条未在 logcat 中打印的短信日志时。我认为收到短信时 class 不是 运行。我在这里添加 BroadcastReceiver class 和 manifest.
public class SmsFilter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG","check if this class runs or not");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
Log.i("payam: ", "sms recived");
Log.i("payam: ", sender);
if (sender != null && Search.search(sender)==true) {
abortBroadcast();
}
return;
}
}
// ...
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="block.list"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FirstPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="@string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="@string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="@string/app_name" >
</activity>
</application>
您需要添加以下权限 -
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
这是一个工作教程Link