Dev*_*man 19 gps android broadcastreceiver
我想阻止用户更改我的应用程序中的WiFi,GPS和加载设置.用户在运行我的应用程序时无需打开/关闭WiFi和GPS.(来自通知栏).是否BroadcastReceiver存在用于收听GPS开/关的功能?
sud*_*007 32
好吧,我做了很多挖掘,发现addGpsStatusListener(gpsStatusListener)在API 24 中 已经弃用了.对我来说,这甚至都没有用!所以,这是另一个替代解决方案.
如果在您的应用中,您想要收听GPS状态更改(我的意思是用户开启/关闭).使用广播肯定是最好的方法.
执行:
/**
* Following broadcast receiver is to listen the Location button toggle state in Android.
*/
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
// Make an action or refresh an already managed state.
}
}
};
Run Code Online (Sandbox Code Playgroud)
不要忘记在Fragment/Activity Lifecycle中有效注册和注销.
registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
Run Code Online (Sandbox Code Playgroud)
例如,如果您使用此a Fragment,请在onResume注册中注册并取消注册onDestroy.此外,如果您要将用户指向设置以启用位置开关,则取消注册onStop将无法运行,因此,您的活动将进入onPause并且片段已停止.
那么这个解决方案可能有很多答案,但是这个解决方案很容易管理和使用.如果有的话,提出您的解决方案
lux*_*xer 22
您可以使用GpsStatus.Listener监听GPS状态,并使用LocationManager进行注册.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
switch(event)
{
case GPS_EVENT_STARTED:
// do your tasks
break;
case GPS_EVENT_STOPPED:
// do your tasks
break;
}
}
});
Run Code Online (Sandbox Code Playgroud)
您需要访问上下文(例如,在"Activity"或"Application"类中).
LocationManager.PROVIDERS_CHANGED_ACTION在您的活动onResume()方法中收听事件:
IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
mActivity.registerReceiver(gpsSwitchStateReceiver, filter);
Run Code Online (Sandbox Code Playgroud)
将此实例添加BroadcastReceiver到您的活动中:
private BroadcastReceiver gpsSwitchStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled || isNetworkEnabled) {
// Handle Location turned ON
} else {
// Handle Location turned OFF
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
在您的活动onPause()方法中取消注册接收器:
mActivity.unregisterReceiver(gpsSwitchStateReceiver);
Run Code Online (Sandbox Code Playgroud)
在Kotlin中试试这个:
添加一个扩展BroadcastReceiver的类:
class GPSCheck(private val locationCallBack: LocationCallBack) :
BroadcastReceiver() {
interface LocationCallBack {
fun turnedOn()
fun turnedOff()
}
override fun onReceive(context: Context, intent: Intent) {
val locationManager =
context.getSystemService(LOCATION_SERVICE) as LocationManager
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) locationCallBack.turnedOn() else locationCallBack.turnedOff()
}
}
Run Code Online (Sandbox Code Playgroud)
然后作为示例以这种方式使用它:
class MainActivity :AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
registerReceiver(GPSCheck(object : GPSCheck.LocationCallBack {
override fun turnedOn() {
Log.d("GpsReceiver", "is turned on")
}
override fun turnedOff() {
Log.d("GpsReceiver", "is turned off")
}
}), IntentFilter(LocationManager.MODE_CHANGED_ACTION))
}}
Run Code Online (Sandbox Code Playgroud)
这不可能。您无法随心所欲地控制/限制硬件的状态。这在 API 中存在是很危险的,因此不存在这样的 API。
| 归档时间: |
|
| 查看次数: |
27536 次 |
| 最近记录: |