问题描述
我使用的位置收听的广播接收器,但是当活动是在别的前景时,我没有使用这个程序,这个广播接收器只工作那么接收者不活跃
这里是活动的onCreate的方法,code我在哪里注册我的广播接收机
Hi i am using a broadcast receiver for location listening but this broadcast receiver is working only when activity is in foreground else when i am not using this app then receiver is not activehere is code of activity onCreate's method where i am registering my broadcast receiver
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);
和code为广播接收器是
and code for BroadcastReceiver is
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方法注销它,然后它得到未经注册意味着它不会工作,我希望我的广播接收器为所有time.can你的工作,请建议如何做到这一点。
i read some where that register and unregister it in onResume and onPause method respectively but when i unregister it in onPause method then it get unregistered means it will not work i want my broadcast receiver to work for all the time.can you please suggest how to accomplish this.
推荐答案
如果你想要这个工作在后台的话,您需要添加广播接收器与广播接收器类的清单文件。
If you want this works in background then, You need to add broadcast receiver in manifest file with Broadcast receiver class.
这样的教程
您可以使用自己的MyReceiver。
You can use your own "MyReceiver".
在这里,在这种情况下,你不需要注册和未注册。这将自动工作。
Here in this case you dont need to register and unregistered. This will work automatically.
这篇关于广播接收器激活,只有当活动正在运行其他停用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!