2

在我的应用程序中,我需要转到手机的设置活动以激活 GPS,并希望使用以下代码返回我的应用程序:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent);

但是当我使用它时,我无法返回我的应用程序。当我按下后,我尝试了这个:

 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, 0);

然后我在这段代码中调用了所有类型的结果

 public void onActivityResult(int requestCode, int resultCode, Intent x){
    if(requestCode == 0){
        if(resultCode == RESULT_CANCELED){
            Button b = (Button)findViewById(R.id.button1);
            b.setText("ON");
            b.setTextColor(0xff2d9ede);
            Intent k = new Intent(getApplicationContext(), serve.class);
            startService(k);    
        }
    }

    if(resultCode == RESULT_FIRST_USER){
        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        Intent k = new Intent(getApplicationContext(), serve.class);
        startService(k);    
    }

    if(resultCode == RESULT_OK){
        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        Intent k = new Intent(getApplicationContext(), serve.class);
        startService(k);    
    }
}
}

但这也不起作用..同样的事情发生了(没有回到我的应用程序).​​.任何想法??... 提前致谢

这是整个活动的代码:

public class Layout2Activity extends Activity {
static String username4 ;
private static final int DIALOG_ALERT = 10;
private static final int DIALOG_ALERT2 = 8;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        showDialog(DIALOG_ALERT);

        return true;
    }

}
return super.onKeyDown(keyCode, event);
}
boolean isGPSEnabled = false;


boolean isNetworkEnabled = false;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
     ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);


    SharedPreferences name = getSharedPreferences("user", MODE_PRIVATE);
    username4 = name.getString("name"+Passwardentry.usernomain,"Admin");

    SharedPreferences p = getSharedPreferences("user", MODE_PRIVATE);
    String number = p.getString("mob"+Passwardentry.usernomain, "please assign no.");

    SharedPreferences last = getSharedPreferences("user", MODE_PRIVATE);
    SharedPreferences.Editor edit = last.edit();
    edit.putString("last number", number);
    edit.commit();

    TextView k = (TextView) findViewById(R.id.textView2);
    k.setText(username4);

    TextView t = (TextView) findViewById(R.id.textView4);
    t.setText(number);
    if (serve.isStarted) {
        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        //TextView n = (TextView) findViewById(R.id.textView5);
        // n.setTextColor(0x7fff00); leh el 2alwan mesh zahra ??
        //n.setText("  On  ");

    } else {
        //TextView n = (TextView) findViewById(R.id.textView5);
        // n.setTextColor(0xff0000);
        //n.setText("  Off  ");
        Button b = (Button)findViewById(R.id.button1);
        b.setText("OFF");
        b.setTextColor(Color.rgb( 139,137,137   ));
    }



}

  public void userchange(View v) {
    Intent i = new Intent(getApplicationContext(), setuserc.class);
    startActivity(i);

}

   public void servicebutton(View V) {

      if (serve.isStarted) {
        //TextView n = (TextView) findViewById(R.id.textView5);
        // n.setTextColor(0xff0000);
        //n.setText("  Off  ");
        Button b = (Button)findViewById(R.id.button1);
        b.setText("OFF");
        b.setTextColor(Color.rgb( 139,137,137   ));
        Intent i = new Intent(getApplicationContext(), serve.class);
        stopService(i);

    } else {
        //TextView n = (TextView) findViewById(R.id.textView5);
        // n.setTextColor(0x7fff00);
        //n.setText("  On  ");


        LocationManager locationManager = (LocationManager)    getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if(isGPSEnabled && isNetworkEnabled){

        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        Intent k = new Intent(getApplicationContext(), serve.class);
        startService(k);}
            else {
                showDialog(DIALOG_ALERT2);



            } 

    }

}

  public void readingsbutt(View V) {
    Intent i = new Intent(getApplicationContext(), Readings.class);
    startActivity(i);
}

    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_intro, menu);
    super.onCreateOptionsMenu(menu);
    // int group1 = 1;
    // MenuItem infoBtn = menu.add(group1,1,1,"About");
    // MenuItem mn = menu.add(group1, 2, 2, "arm");
    // mn.setIcon(R.drawable.set);
    // infoBtn.setIcon(R.drawable.set);

     //int group2=2; 
    // MenuItem appSettings = menu.add(group2,3,2,"Application Settings");
    // appSettings.setIcon(R.drawable.set);



     return true;
}

  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_settings) {
        Intent i = new Intent(getApplicationContext(), settings.class);
        startActivity(i);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
    }
    if (item.getItemId() == R.id.help) {
        Intent i = new Intent(getApplicationContext(), help.class);
        startActivity(i);
    }
    if (item.getItemId() == R.id.about) {
        Intent i = new Intent(getApplicationContext(), Info.class);
        startActivity(i);
    }
     //switch(item.getItemId()) {
    // case 1:
    //    break;
    //  case 2:
    //    break;
    // }

    return true;
}



   @Override
    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_ALERT:
      // Create out AlterDialog
      Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("Are you sure you want to Quit ?");
      builder.setCancelable(true);
      builder.setPositiveButton("NO", new OkOnClickListener());
      builder.setNegativeButton("Yes", new CancelOnClickListener());
      AlertDialog dialog = builder.create();
      dialog.show();


    case DIALOG_ALERT2:
          // Create out AlterDialog
          Builder builder2 = new AlertDialog.Builder(this);
          builder2.setMessage("No location service available please activate network and satellite GPS ");
          builder2.setCancelable(true);
          builder2.setPositiveButton("Cancel", new OkOnClickListener2());
          builder2.setNegativeButton("Settings", new CancelOnClickListener2());
          AlertDialog dialog2 = builder2.create();
          dialog2.show();
    }
    return super.onCreateDialog(id);
  }

    private final class CancelOnClickListener implements
      DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {

        setResult(0);
        finish();
        System.exit(0);// /el 2aaady


    }}
    private final class OkOnClickListener implements
     DialogInterface.OnClickListener {
     public void onClick(DialogInterface dialog, int which) {



  }
    }

    private final class CancelOnClickListener2 implements
    DialogInterface.OnClickListener {
  public void onClick(DialogInterface dialog, int which) {
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, 0);



   }}
  private final class OkOnClickListener2 implements
 DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {



}
}

public void onActivityResult(int requestCode, int resultCode, Intent x){
    if(requestCode == 0){
        if(resultCode == RESULT_CANCELED){
            Button b = (Button)findViewById(R.id.button1);
            b.setText("ON");
            b.setTextColor(0xff2d9ede);
            Intent k = new Intent(getApplicationContext(), serve.class);
            startService(k);    
        }
    }

    if(resultCode == RESULT_FIRST_USER){
        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        Intent k = new Intent(getApplicationContext(), serve.class);
        startService(k);    
    }

    if(resultCode == RESULT_OK){
        Button b = (Button)findViewById(R.id.button1);
        b.setText("ON");
        b.setTextColor(0xff2d9ede);
        Intent k = new Intent(getApplicationContext(), serve.class);
        startService(k);    
    }
  }
 }
4

3 回答 3

2

下面对我有用。

package com.example.test;

import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
boolean firstTime = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("test", "onCreate");
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener(){

        @Override
    public void onClick(View arg0) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent);
        }
    });
}

@Override
protected void onResume(){
    super.onResume();
    Log.i("test", "onResume");
    if (firstTime){
    Log.i("test", "it's the first time");
    firstTime = false;
    }

    else{
    Log.i("test", "it's not the first time");

    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

于 2013-03-25T18:18:33.510 回答
1

我在Android 7.0 Nougat上遇到了同样的问题。

解决方案是简单地添加到清单:

<uses-feature android:name="android.hardware.location.gps" />

然后在关闭设置活动后,我的呼叫活动又回来了。

于 2017-09-04T09:06:24.660 回答
0

我明白你的意思,几天前我也遇到了同样的问题。我也在这里回答了类似的问题

我只是再次在这里发布我的答案。

在您的情况下,您不应该使用 onActivityResult,因为位置源设置活动永远不会为您设置结果代码。

为了实现你想要的,你应该在你的 onResume() 方法中做一些事情。GPS 位置源设置活动完成后,将调用您的 onResume() 方法。因此,您只需在 onResume 中检查 GPS 的开启或关闭。

注意,当你第一次启动应用程序时,onResume 会在 onCreate 之后被调用。因此,您应该决定在 onCreate 之后或 GPS 设置活动之后调用 onResume。

下面是一个示例代码

void onCreate(){
    //do something 
    Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
 }

void onResume(){
    if (firstTime){
  // do something
    firstTime = false;
    }
    else{ // it's from GPS setting activity
     //check your gps again with your logic here
    }
 }
于 2013-03-25T16:49:08.347 回答