【问题标题】:can't return to activity after System settings intent系统设置意图后无法返回活动
【发布时间】:2013-03-25 15:17:00
【问题描述】:

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

【问题讨论】:

  • 关闭系统设置后你看到了什么视图,如果不是你的应用程序。
  • 主菜单或任何其他活动应用
  • @YoussefElHemali:请添加带有问题的完整活动代码以获得更多帮助

标签: java android android-intent settings


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案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;
    }
    

    }

    【讨论】:

      【解决方案3】:

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

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

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

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

      注意,当你第一次启动应用时,onResume 会在 onCreate 之后被调用。所以你应该决定 onResume 是在 onCreate 之后还是在 GPS 设置活动之后调用。

      下面是示例代码

      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
          }
       }
      

      【讨论】:

      • sry,这不起作用,结果相同。离开了应用程序。永远不会回来
      • 设置活动完成后,你的onResume()被调用了吗?
      • 注意:在第一次崩溃时尝试了 2 次代码并要求 onResume 是超级的,所以我尝试了这一行 super.onResume(); ..会有什么不同吗??抱歉,我是 android 新手 .. 无论如何感谢您的关注
      • 首先确保当设置活动返回时,您的 onResume() 方法被调用。根据我对 Android 的理解,在你的情况下应该调用它。尝试在 onResume() 中打印一些内容。
      • 在 firstTime = false 之后为简历做了 2 次祝酒 1 次;命名为 resume1 这个 1 在 else 调用 resume2 之后工作了另一个,但没有出现
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多