您的位置:首页 > 移动开发 > Android开发

[Android系统]状态栏上定位状态获取

2015-01-14 22:12 936 查看
1.监听广播:

IntentFilter filter = new IntentFilter();
//监听使能状态监听广播

filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);
//监听gps定位广播

filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);

context.registerReceiver(this, filter);
2.接收回调:

在onReceive中添加代码:

获取GPS开关状态

final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);

boolean visible;

int iconId, textResId;

if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {

// GPS is getting fixes

//如果当前为gps定位广播

iconId = com.android.internal.R.drawable.stat_sys_gps_on;

textResId = R.string.gps_notification_found_text;

visible = true;

} else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {

// GPS is off

GPS关闭,没有图标显示

visible = false;

iconId = textResId = 0;

} else {

// GPS is on, but not receiving fixes

GPS打开,但是为定位,也就是我们常看到的定位图标闪动

iconId = R.drawable.stat_sys_gps_acquiring_anim;

textResId = R.string.gps_notification_searching_text;

visible = true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: