我希望制作一个可以根据某些参数截取短信的应用。截至目前,我已经注册了一个切换按钮以及SMSReceiver类,但是我无法找到一种方法,只有在启用切换功能时SMSReceiver才能工作,并且当禁用切换时SMSReceiver停止工作。
Main.Java
public class Main扩展Activity {
Intent in = new Intent();
IntentFilter intentFilter;
String drivingMessage;
String originAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
}
BroadcastReceiver intentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
originAddress = intent.getExtras().getString("sms");
Toast.makeText(context, originAddress, Toast.LENGTH_LONG).show();
}
};
@Override
protected void onResume(){
registerReceiver(intentReceiver, intentFilter);
super.onResume();
}
@Override
protected void onPause(){
unregisterReceiver(intentReceiver);
super.onPause();
}
public void serviceEnabled(View view){
ToggleButton tB = (ToggleButton) findViewById(R.id.serviceToggle);
boolean on = tB.isChecked();
if(on){
Toast.makeText(this, " toggle is on ", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, " toggle is off " , Toast.LENGTH_SHORT).show();
}
}
}
SMSReceiver.Java
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "SMS From";
if(bundle != null){
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]);
if (i == 0){
str += msgs[i].getOriginatingAddress();
}
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
this.abortBroadcast();
}
}
非常感谢任何帮助,因为这是一个班级的项目。提前谢谢!
答案 0 :(得分:1)
由于您的切换按钮是一种用户首选项/设置,您可以将状态保存为SharedPreferences中的布尔标志。
然后在您的SMSReceiver中,在处理传入消息之前,您可以从SharedPreferences检查状态标志。然后继续进行。
public void serviceEnabled(View view){
ToggleButton tB = (ToggleButton) findViewById(R.id.serviceToggle);
boolean on = tB.isChecked();
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(this);
Editor editor=preferences.edit();
editor.putBoolean("is_sms_on", on);
editor.commit();
if(on){
Toast.makeText(this, " toggle is on ", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, " toggle is off " , Toast.LENGTH_SHORT).show();
}
}
SMSReceiver.Java
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context)
boolean isServiceOn=preferences.getBoolean("is_sms_on",true);
if(isServiceOn){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "SMS From";
if(bundle != null){
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]);
if (i == 0){
str += msgs[i].getOriginatingAddress();
}
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
this.abortBroadcast();
}
}
}
答案 1 :(得分:0)
取消选中切换按钮时,请尝试取消注册接收器。
public void serviceEnabled(View view){
ToggleButton tB = (ToggleButton) findViewById(R.id.serviceToggle);
boolean on = tB.isChecked();
if(on){
//your code
} else{
unregisterReceiver(intentReceiver);
}
}
答案 2 :(得分:0)
当取消选中切换按钮时,尝试直接从清单中禁用接收器,接收器根本不在图片中。在服务启用功能的else部分使用此功能
PackageManager pm=getPackageManager();
ComponentName componentName=new ComponentName(getApplicationContext(),yourreceiver.class);//where yourreceiver is the name of your receiver class
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);