如何在广播接收器内使用上下文?

时间:2014-02-14 17:29:04

标签: android broadcastreceiver android-context

我设法创建了一个返回活动上下文的函数,我在广播接收器中使用了这个上下文,它完成了它应该做的事情,如果应用程序没有停止,但是当我停止应用程序时,我无法得到活动的上下文,因为它返回null值,那么即使我的应用程序被杀死,如何保持广播接收器使用的上下文值?

这是返回Context对象的函数,并且在主要活动的OnCreate()内调用构造函数:

public class ContextGen {
static Context conGen = null;

public ContextGen(Context context) {
    conGen = context;

}

public static Context returnContextGen() {

    return conGen;
}

}

这是检查传入短信的广播接收器:

public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
@Override
    public void onReceive(Context context, Intent intent) {
        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();
        // Context conOfMain = ContextGen.returnContextGen();

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage
                        .createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage
                        .getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                // what i added to the code

                Log.i("SmsReceiver", "senderNum: " + senderNum
                        + "; message: " + message);

                // Show Alert
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "
                        + senderNum + ", message: " + message, duration);
                toast.show();

                if (message.equalsIgnoreCase("PI lock")) {
                    System.out
                            .println("lock entereeeeed =====================");
                    Context conOfMain = ContextGen.returnContextGen();
                    System.out.println("Context is" + conOfMain);
                    LockDevice lock = new LockDevice(conOfMain);
                    lock.lockDeviceNow();
                    abortBroadcast();

    }}}}}

2 个答案:

答案 0 :(得分:3)

第1步:

import android.content.BroadcastReceiver;
import android.content.IntentFilter;

导入您的活动。

第2步:

在你的活动中写下这个

    public BroadcastReceiver intentReceiver = new BroadcastReceiver(){
          @Override
           public void onReceive(Context context,Intent intent){
        System.out.println("inside onReceive");

        yourfunction();
    }    
};

第3步: 在ocreate()中写

    intentfilter = new IntentFilter();
    intentfilter.addAction("SMS_RECEIVED_ACTION");

第4步:

BroadCastReceiver类中的

包含以下代码

   Intent broadCastIntent = new Intent();
   broadCastIntent.setAction("SMS_RECEIVED_ACTION");
   context.sendBroadcast(broadCastIntent);

希望这会对你有帮助..

答案 1 :(得分:2)

不要像现在这样严厉,只需使用扩展你的Application的Singleton。像这样:

import android.app.Application;
import android.content.Context;

public class MyAppContext extends Application {
  private Context context;

  public Context getContext() { return context; }

  public void setContext(Context context_) { context = context_; }

  public void onCreate(){
    super.onCreate();
    this.context = getApplicationContext();
  }  
}

之后,如果您想获得上下文,只需使用:

MyAppContext myContextManager = ((MyAppContext) getApplicationContext());

----编辑----

创建MainActivity后,只需致电:

myContextManager.setContext(this);

你只做一次。下次你只需要得到它。在你的BroadcastReceiver中,你将无法做到这一点,所以在定义之前先获取它并将其存储在变量中(例如,称为myContextManager),然后在内部执行以下操作: / p>

Context context = myContextManager.getContext();