如何取消重写 BroadcastReceiver 中的 onReceive 方法

发布于 2024-12-18 00:43:59 字数 2939 浏览 0 评论 0原文

我有一个应用程序,可以使用默认消息回复任何收到的短信。该应用程序可以运行,但是当我关闭应用程序时它也可以运行。无论如何,我是否可以取消重写 BroadcastReceiver 的 onReceive 方法,以便一旦我关闭应用程序,它就不会使用我的 onReceieve 方法版本?如果没有,解决这个问题的最佳方法是什么?

//SMS.java
package net.learn2develop.SMSMessaging;

import net.learn2develop.SMSMessaging.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SMS extends Activity 
{
    Button btnExit;
    static EditText txtMessage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        btnExit = (Button) findViewById(R.id.btnExit);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnExit.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {                
                finish();
            }
        });        
    }    

    public static String getMessage(){
        return txtMessage.getText().toString();
    }
}
______________________________________________________________________________________
//SMSReceiver.java
package net.learn2develop.SMSMessaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---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";   
                sendSms(msgs[i].getOriginatingAddress(),SMS.getMessage());
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }         
    }
    private void sendSms(String phonenumber,String message)
    {
      SmsManager manager = SmsManager.getDefault();
      manager.sendTextMessage(phonenumber, null, message, null, null);
    }
}

I have an app that replies to any received SMS with a default message. The app works, however it also works when I close the app. Is there anyway I can un-override the onReceive method for BroadcastReceiver, so that once I close the app, it won't use my version of the onReceieve method? If not, what's the best way to fix this problem?

//SMS.java
package net.learn2develop.SMSMessaging;

import net.learn2develop.SMSMessaging.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SMS extends Activity 
{
    Button btnExit;
    static EditText txtMessage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        btnExit = (Button) findViewById(R.id.btnExit);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnExit.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {                
                finish();
            }
        });        
    }    

    public static String getMessage(){
        return txtMessage.getText().toString();
    }
}
______________________________________________________________________________________
//SMSReceiver.java
package net.learn2develop.SMSMessaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---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";   
                sendSms(msgs[i].getOriginatingAddress(),SMS.getMessage());
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }         
    }
    private void sendSms(String phonenumber,String message)
    {
      SmsManager manager = SmsManager.getDefault();
      manager.sendTextMessage(phonenumber, null, message, null, null);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-12-25 00:43:59

我只是创建一个布尔值来决定是否执行您的代码或超级的 onRecieve 方法。然后只需重写 Activity 的 onPause 方法并切换布尔标志即可。

I would just create a boolean which decides whether to execute your code or the super's onRecieve method. Then just override the onPause method of the activity and toggle the boolean flag.

宣告ˉ结束 2024-12-25 00:43:59

尝试使用 SharedPreference,它是一个布尔值,用于说明应用程序是否正在运行。

在 onCreate() 中,将布尔值设置为 true,并重写 onDestroy() 将其设置为 false。

然后,在发送短信之前,检查布尔值是 true 还是 false。检查这一点的唯一方法是通过服务,因此您需要在 服务

Try having a SharedPreference, a boolean that states whether the application is running or not.

In the onCreate(), have the boolean set as true, and override the onDestroy() to set it to false.

Then, before sending the sms, check whether the boolean is true or false. The only way to check this is through a service, so you will need to have the sendSms method within a Service

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文