2016-12-29 5 views
1

をオンにしながら、これは私のコードである:例外GPS

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", true); 
     sendBroadcast(intent); 
    } 
} 

これはlogcatの誤差である:

致命的な例外:メイン プロセス:com.example.satyajittarafdar.gps_on_automatic、 PID:20469 java.lang.RuntimeException:アクティビティを開始できません ComponentInfo {com.example.satyajittarafdar.gps_on_automatic/com.example.satyajittarafdar.gps_on_a utomatic.MainActivity}: java.lang.SecurityException:許可拒否:PID = 20469から 放送android.location.GPS_ENABLED_CHANGEを送信することを許可されていない、 UID = 10240 でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java。 2429) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493で) android.app.ActivityThread $ H.handleMessageでandroid.app.ActivityThread.-wrap11(ActivityThread.java) で (ActivityThread.java :1357)android.os.Handler.dispatchMessage(Handler.java:102)の android.os.Looper.loopで でjava.lang.reflect.Method.invoke(ネイティブメソッド) でandroid.app.ActivityThread.mainで(Looper.java:148) (ActivityThread.java:5459) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 原因:java.lang.SecurityException:許可の拒否:許可されていません ブロードキャストandroid.location.GPS_ENABLED_CHANGEからpid = 20469、 uid = 10240 android.os.Parcel.readException(Parcel.java:1620)android.app.ContextImpl.sendBroadcast(ContextImpl.java:767)で android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3128)でandroid.os.Parcel.readException(Parcel.java:1573) で でandroid.app.Activity.performCreateでandroid.content.ContextWrapper.sendBroadcast(ContextWrapper.java:396) でcom.example.satyajittarafdar.gps_on_automatic.MainActivity.onCreate(MainActivity.java:19) (Activity.java :6259) at android.app.Instrumentation.callActivityOnCreate(Instrumentatio n.java:1130) でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) でandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) android.app.ActivityThread.-wrap11でandroid.os.Looper.loopでandroid.os.Handler.dispatchMessage(Handler.java:102) で android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1357) で(ActivityThread.java) ( Looper.java:148) android.app.ActivityThread.main(ActivityThread。Javaの:5459) java.lang.reflect.Method.invoke(ネイティブメソッド) で com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) でcom.android.internalで。 os.ZygoteInit.main(ZygoteInit.java:618)

答えて

0

アンドロイドバージョン4.4からプログラムでgpsの状態を変更することはできません。

良い説明理由:あなたがそうphoen設定にユーザーを送信するために意図を使用して手動で有効にするために彼を要求している必要がありますどのようなRead this post

+0

しかし、デフォルトのGoogleマップを使用しながら、ウルGPSがオフの場合、それは自動的にのみ、アラートダイアログMSGを示すONします...それをONにする設定にはなりません。 –

+0

これはON GPSを手動で実行するコードで、完全に動作します。 Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(myIntent); しかし、私は設定でそれをオンにしたくない。 コードがある場合は、手伝ってください。 Thanx事前に –

0

これは公開APIとして利用できないため、Android 4.4以降からプログラムでGPS状態を変更することはできません。インテントを準備し、ユーザーを設定にリダイレクトします。ここ

startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 

は、いくつかの参照です:

https://stackoverflow.com/a/22529296/3758024

https://stackoverflow.com/a/20236331/3758024

+0

しかし、デフォルトのGoogleマップを使用している間は、GPSがオフの場合、アラートダイアログメッセージのみを表示して自動的にオンになります...設定には入りません –

+0

これは手動でgpsをオンにして、その作業を完全に行いますIntent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(myIntent);しかし、私は設定でそれをオンにしたくない。コードがあれば教えてください。あらかじめThanx –

関連する問題