Android 位置服务启动问题
Android Location Service Launch Issue
它应该做的是在需要时打开定位,在不需要时关闭定位。当我点击打开定位服务时,我的定位服务出现问题,它说,"app has stopped" 请在下面找到我的 logcat。非常感谢任何帮助。谢谢
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kenpar.dmsassign2, PID: 21749
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=21749, uid=10184
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2640)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1499)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:377)
at com.kenpar.dmsassign2.LocationActivity.onClick(LocationActivity.java:30)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19274)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
位置java代码:
public class LocationActivity extends Activity {
Button onLoc;
Button offLoc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
onLoc = (Button)findViewById(R.id.onLoc);
offLoc = (Button)findViewById(R.id.offLoc);
onLoc.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent = new Intent ("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
});
offLoc.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在 android 版本 4.4 及更高版本中,您无法手动打开和关闭定位服务。它会抛出 securityException
并且只能手动完成。您可以做什么,当需要位置服务时,您可以通过触发此意图将用户移动到位置服务设置屏幕:
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
它应该做的是在需要时打开定位,在不需要时关闭定位。当我点击打开定位服务时,我的定位服务出现问题,它说,"app has stopped" 请在下面找到我的 logcat。非常感谢任何帮助。谢谢
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kenpar.dmsassign2, PID: 21749
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=21749, uid=10184
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2640)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1499)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:377)
at com.kenpar.dmsassign2.LocationActivity.onClick(LocationActivity.java:30)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19274)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
位置java代码:
public class LocationActivity extends Activity {
Button onLoc;
Button offLoc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
onLoc = (Button)findViewById(R.id.onLoc);
offLoc = (Button)findViewById(R.id.offLoc);
onLoc.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent = new Intent ("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
});
offLoc.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在 android 版本 4.4 及更高版本中,您无法手动打开和关闭定位服务。它会抛出 securityException
并且只能手动完成。您可以做什么,当需要位置服务时,您可以通过触发此意图将用户移动到位置服务设置屏幕:
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));