系统设置意图后无法返回活动

时间:2013-03-25 15:17:01

标签: java android android-intent settings

在我的应用中,我需要去设置手机的活动以激活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);    
    }
  }
 }

3 个答案:

答案 0 :(得分: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;
}

}

答案 1 :(得分:0)

我理解你的意思,几天前我遇到了同样的问题。我也在here

回答了类似的问题

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

在这种情况下,您不应该使用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
    }
 }

答案 2 :(得分:0)

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

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

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

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