我在我的项目中使用BroadcastReceiver
隐藏短信我有两个按钮用于注册,一个用于取消注册广播接收器。
这是问题所在 - 当我按下按钮注册其开启广播但是当短信是接收器时,代码的功能不起作用它不会隐藏SMS。
在接收器代码中我正在检查bundle值是否不等于null然后只有代码才会执行隐藏部分现在我的问题是如何在sms开始接收时更改bundle值或者是否有任何方法听incommig sms
我正在提供完整的代码,请任何人找到解决方案
package sam.ll;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class samm extends Activity {
Button b1,b2;
BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("calling me "," !!!");
System.out.println("podaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
System.out.println("bundle value issss"+bundle);
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
abortBroadcast();
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
};
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
samm.this.registerReceiver(mReceiver, intentFilter);
Intent i = new Intent("android.intent.action.MAIN");
sendBroadcast(i);
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
unregisterReceiver(mReceiver);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="Button"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
//的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sam.ll"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".samm"
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=".mReceiver" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</application>
</manifest>
答案 0 :(得分:3)
点击b1替换代码
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
samm.this.registerReceiver(mReceiver, intentFilter);
}
});
答案 1 :(得分:0)
当您单击第一个按钮时,接收器已注册以侦听由您自己的代码创建的意图“android.intent.action.MAIN”,并且没有附加任何捆绑(您不附加任何内容)。不要订阅这个虚假的意图,也不要将它发送给你自己的广播接收器。
要订阅收到的短信,您需要听取其他意图:
IntentFilter intentSMSReceiver = new IntentFilter();
intentSMSReceiver.addAction("android.intent.action.DATA_SMS_RECEIVED");
intentSMSReceiver.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mReceiver, intentSMSReceiver);
当您的设备收到短信时会发出此意图。