Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

trouble using Airplane Mode on Android

I have troube to use Airplane mode on my app. The app runs perfectly well on my emulator but not in my real phone when I try my apk.

Here is the code to activate the Airplane :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        //intent.putExtra("state", !isEnabled);
        intent.putExtra("state", true);
        sendBroadcast(intent);

To desactivate it :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        intent.putExtra("state", false);
        sendBroadcast(intent);

I read that I cannot create the intent (android doc) but I saw people that could make airplane mode so I don't understand!

Thanks to help me !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You CANNOT broadcast a System level intent. This is illegal by android's standards.

Instead of trying to send the intent, why don't you just implement a broadcast reciever to listen to that specific action and then when it changes, have your application handle it. This would be a much better approach. You cannot and should NOT expect to control airplane mode, but your app can listen to when it is invoked. You can also retrieve a property from the System database that tells you if its in airplane mode.

Once again Do not focus on trying to control it, just handle it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...