打开和关闭广播接收器在Android的按钮点击 [英] Turn on and off broadcast receiver on button click in android
问题描述
我这样做是广播接收短信隐藏项目
在code以下
i am doing an sms hiding project which is broadcast receiver the code is given below
package com.sms.sms;
public class ReceiverClass extends BroadcastReceiver
{
SQLiteDatabase DiaryDB = null;
String message,number;
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[ ] msgs = null;
String str = "";
if (bundle != null)
{
abortBroadcast();
//---retrieve the received message here ---
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";
message = msgs[i].getMessageBody().toString();
number = msgs[i].getOriginatingAddress();
}
// ........first show sms here.....
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
String name = findNameByAddress(context, number);
if(name.equals(number))
name = "Unknown";
DiaryDB = context.openOrCreateDatabase("DIARY_DATABASE", context.MODE_PRIVATE, null);
DiaryDB.execSQL("CREATE TABLE IF NOT EXISTS Messages (TIMESTAMP DATE DEFAULT (DATETIME('now','localtime')), MESSAGE varchar, SENDER varchar, NAME varchar);");
System.out.println("table createdddddddddddddddddddddddddd");
DiaryDB.execSQL("INSERT INTO Messages(MESSAGE,SENDER,NAME) VALUES('" + message +"','"+ number +"','"+ name +"')");
DiaryDB.close();
updateName(context,name, number);
}
}
public String findNameByAddress(Context ct,String address)
{
Uri myPerson = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(address));
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = ct.getContentResolver().query(myPerson, projection, null, null, null);
if (cursor.moveToFirst())
{
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("","Found contact name");
cursor.close();
return name;
}
cursor.close();
Log.e("","Not Found contact name");
return address;
}
public void updateName(Context ct, String name, String sender)
{
DiaryDB = ct.openOrCreateDatabase("DIARY_DATABASE", ct.MODE_PRIVATE, null);
DiaryDB.execSQL("UPDATE Messages SET NAME='"+name+"' WHERE SENDER='"+sender+"'");
DiaryDB.close();
}
我有两个按钮ON和OFF在我的主要活动。我需要的是,当我preSS ON按钮就应该启动Bordcast接收器和应该开始隐藏短信,当我preSS OFF按钮,我需要停止(广播接收器)隐藏消息过程中还是应该获取短信回到收件箱*的(当广播接收机得到关闭,将发生)的*。现在,我怎么和关闭Boardcast接收过程中,请帮助
I had two button ON and OFF in my main Activity. What I need is that when i press the ON button it should Start the Bordcast receiver and should start hide sms and when i press the OFF button i need to stop (Broadcast receiver)hiding messages process or should get sms back to the inbox*(which will occur when the broadcast receiver get turned off)*. now how can i on and off a Boardcast receiver process please help
答案后版
我的活动类//
package com.an.oid;
public class OnoffActivity extends Activity {
int count =0;
Button a,b;
ReceiverClass rc ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a=(Button)findViewById(R.id.button1);
b=(Button)findViewById(R.id.button2);
rc= new ReceiverClass();
a.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
System.out.println("onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
registerReceiver(rc,filter);
}
});
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
unregisterReceiver(rc);
}
});
}
}
我的清单//
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.an.oid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".OnoffActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
越来越雁之后,我试过,但它不工作?
After getting anser i tried this but it is not working?
推荐答案
这如果您注册了在活动水平(不舱单)接收器使用是唯一可能的。
This is only possible if you register your receiver at Activity level(not Manifest) using
registerReceiver(BroadcastReceiver, IntentFilter)
和您可以使用它注销按钮的点击。
and on the click of the button you can Unregister it using..
unregisterReceiver(BroadcastReceiver receiver)
修改II
//注册按钮
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
IntentFilter filter = new IntentFilter(MY_ACTIVITY);
Sms2Activity rc = new Sms2Activity();
registerReceiver(rc,filter);
}
});
//注销按钮
unregister.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
unregisterReceiver(rc);
}
});
这篇关于打开和关闭广播接收器在Android的按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!