我有一个非常简单的需求。我想检查用户是否启用了使用网络位置复选框。如果启用,我想将用户传递给一个名为 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();
}
}