0

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




    }

}
4

1 回答 1

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;
     }
于 2013-08-10T10:40:28.610 回答