我有一个非常简单的需求。我想检查用户是否启用了使用网络位置复选框。如果启用,我想将用户传递给名为LOCATIONPAGE的活动。如果未启用网络位置,我想显示警报。
这听起来很简单,但我想要弄清楚这一点我绝对疯狂。如果我删除支票并将用户直接传递给LOCATIONPAGE活动,一切似乎都有效。任何想法都会非常有用。谢谢!
package com.appname.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gpsoff);
Thread logoTimer = new Thread(){
public void run() {
try{
int logoTimer = 0;
while (logoTimer<6000){
sleep(100);
logoTimer = logoTimer+100;
}
LocationManager locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean networkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!networkEnabled){
AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
}
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
// see http://www.androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button
}
else {
startActivity(new Intent("com.appname.app.LOCATIONPAGE"));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
private LocationManager getSystemService(String locationService) {
// TODO Auto-generated method stub
return null;
}
};
logoTimer.start();
}
}
答案 0 :(得分:1)
如果我正确地告诉你你需要的东西。
我在这里使用共享首选项来满足复选框的需要;
通过以下代码:
prefs.xml
<CheckBoxPreference
android:defaultValue="true"
android:key="alpha"
android:summary="Check/Uncheck"
android:title="Location" />
现在在PrefsActivity中膨胀相同的prefs.xml
现在进入主要活动:
SharedPreferences sp =PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean result = sp.getBoolean("alpha", true);
if (result)
{
if(checkNetwrok())
{
Intent i = new Intent(this, LocationPage.class)
startActivity(i);
{
else
{
Show toast/Dialog with "network not found !"
}
}
else {
msg: pls enable the network checkbox first
}
public boolean checkNetwork(){
LocationManager locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean networkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return networkEnabled;
}