【问题标题】:BroadCast Receiver Active only when activity is running else deactivated广播接收器仅在活动运行时激活,否则停用
【发布时间】:2012-12-14 04:31:42
【问题描述】:

您好,我正在使用广播接收器进行位置侦听,但此广播接收器仅在活动处于前台时才工作,否则当我不使用此应用程序时,接收器未激活 这是我正在注册广播接收器的 onCreate 方法的活动代码

 PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);



locationManager.addProximityAlert(

           latitude, // the latitude of the central point of the alert region

           longitude, // the longitude of the central point of the alert region

           1000, // the radius of the central point of the alert region, in meters

           -1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration

           proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected

      );

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s); 

     registerReceiver(new ProximityAlertReceiver(), filter);

BroadcastReceiver 的代码是

public class ProximityAlertReceiver extends BroadcastReceiver {



    private static final int NOTIFICATION_ID = 1000;
    public static final String PREFS_NAME = "MyPrefsFile";


    @Override

    public void onReceive(Context context, Intent intent) {



        String key = LocationManager.KEY_PROXIMITY_ENTERING;
SharedPreferences shared=context.getSharedPreferences(MainActivity.PREFS_NAME,0);


        Boolean entering = intent.getBooleanExtra(key, false);
Double longitude=intent.getDoubleExtra("longitude", 0.0);
Double latitude=intent.getDoubleExtra("latitude",0.0);

Intent in = new Intent(context,AlarmActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("longitude", longitude);
in.putExtra("latitude", latitude);
Log.i("cont1", shared.getInt("count", 0)+"");
in.putExtra("count",shared.getInt("count", 0));
context.startActivity(in);
}

}

我读了一些在 onResume 和 onPause 方法中分别注册和注销它的地方,但是当我在 onPause 方法中注销它时,它被注销意味着它将无法工作我希望我的广播接收器一直工作。你能建议如何做到这一点。

【问题讨论】:

    标签: android android-intent broadcastreceiver


    【解决方案1】:

    如果你想让它在后台工作,你需要在清单文件中添加广播接收器和广播接收器类。

    喜欢这个教程http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

    您可以使用自己的“MyReceiver”。

    在这种情况下,您不需要注册和取消注册。这将自动运行。

    【讨论】:

    • Yashdeep patel 你的答案是正确的。Ritesh Mehandiratta 试试这个答案。
    • @Ritesh Mehandiratta 如果此答案对您有帮助。请选择此作为答案。谢谢,
    【解决方案2】:

    如果你想让你的广播接收器一直运行,你需要从清单文件中注册它。 来自文档: 您可以使用 Context.registerReceiver() 动态注册此类的实例,也可以通过 AndroidManifest.xml 中的标记静态发布实现。

    http://developer.android.com/reference/android/content/BroadcastReceiver.html

    【讨论】:

      【解决方案3】:

      首先你被定义为 mainfest 所以应用程序也没有运行它们 广播接收器工作..所以你可以处理这种类型的广播 如果启用或禁用,则使用 PackageManager 并获取包名称 您可以检查并运行您的广播

      【讨论】:

        【解决方案4】:

        我认为服务类会帮助你,尝试在后台实现无限次,当你的服务发现某些东西发生了变化时,它会发送一些广播。给你:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多