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

2024-02-17

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

下面对我有用。

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

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

系统设置意图后无法返回活动 的相关文章

随机推荐

  • React Native - 使用 React Navigation 动态创建导航器

    我正在使用 React Native 构建一个移动应用程序 并使用 React Navigation 在我的应用程序中构建一个导航器 React 导航为我提供了一种处理抽屉内嵌套选项卡栏的好方法 该抽屉也在 Stack Navigator
  • java.net.SocketException:Android 模拟器中的协议不支持地址族

    我正在尝试运行简单的应用程序来从 android 模拟器访问互联网 这是我的代码 我在代理后面 并通过 无线网络 gt APN gt 在模拟器中配置了代理设置 但互联网是通过浏览器而不是应用程序运行的 HttpURLConnection c
  • AWS X 射线追踪在 Node.js 中的传出请求上中断

    嘿 我正在尝试跟踪来自 Express 应用程序的传出请求 但我无法让它工作 当我不使用 AWSXRAY captureHttpsGlobal 函数时 传入请求一切正常 我可以在 服务地图 中看到我的应用程序以及 AWS 上传入的传入请求跟
  • 在捆绑包中找不到名为“Main”的故事板

    我收到一个奇怪的错误 Could not find a storyboard named Main in bundle NSBundle 当尝试在真实的 iOS 设备上运行我的应用程序时 我的目录中有该文件 并且它在模拟器中运行良好 然而
  • Intel HD 3700 linux 的 Java 8 Swing 渲染问题

    我在 Kubuntu 18 04 中用 java 8 update 181 开发了一个应用程序 在我的开发 PC 中 我拥有配备英特尔 HD Graphics 530 的英特尔 i3 6100 所有图形均使用 swing 完成 并且通过此硬
  • 使用 HttpEntity 在 Spring RestTemplate 中删除

    我不知道为什么我的代码不起作用 我尝试过 Postman 并且工作正常 但与RestTemplate当它使用相同的端点时我无法得到响应 ResponseEntity
  • 从 Android 中的 EditText 中移除焦点 [重复]

    这个问题在这里已经有答案了 我有两个EditTexts 和一CheckBox and a Button在我的布局中按上述顺序 将值输入到EditText 用户必须通过单击接受条款和条件Checkbox 我需要将焦点从EditText单击该复
  • 如何在 NetBeans 中获得 jQuery 代码完成?

    我在用着适用于 PHP 的 NetBeans http bits netbeans org netbeans 6 8 m2 当我编辑 js 文件时 它给了我javascript代码完成 我怎样才能让它同时给我 jQuery 代码完成 首先转
  • 配置 htaccess 以与 IIS6 上的 IIRF 配合使用 - codeigniter

    我刚刚使用 CodeIgniter 创建了一个网站 问题出在我们的服务器在带有 IIS6 的 Windows Server 2003 上运行 调用网站时只显示首页 其他页面由于IIS6的 htaccess限制而显示404错误页面 支持人员告
  • 如何使用 openssl 创建公钥和私钥?

    我的问题是 How to create 公钥 和 Windows 中 OpenSSL 的私钥 How to put the created public key in crt文件和 私人的 pcks8 file 我想使用这两个密钥在 Jav
  • 无法启动 Groovy 控制台 - 缺少依赖项 javax/xml/bind/Unmarshaller

    常规版本 2 5 1 Java版本 10 0 2 尝试启动 groovyConsole 我得到以下信息 groovyConsole WARNING An illegal reflective access operation has occ
  • 包含容器日志的日志文件在哪里?

    我正在使用几个容器运行docker compose 我可以使用命令查看应用程序日志docker compose logs 但是 我想访问原始日志文件以将其发送到某个地方 例如 它位于哪里 我想每个容器都有单独的日志 在容器内 但我在哪里可以
  • Qprocess 搞乱了我的 linux 命令(我认为)。怎么修? [复制]

    这个问题在这里已经有答案了 我需要强制我的 C QT4 应用程序从 Linux 命令读取结果 我正在尝试使用 Qpr ocess 但是一旦我的命令变得复杂 它就会以某种方式变得混乱 只是猜测 并且不起作用 这里我试着给大家举一个小例子 QP
  • Java打印四字节十六进制数

    我有一个小问题 我有 5421 1 和 1 等数字 我需要以四个字节打印它们 例如 5421 gt 0x0000152D 1 gt 0xFFFFFFFF 1 gt 0x00000001 另外 我有浮点数 如 1 2 58 654 8 25f
  • Jest.js 强制窗口未定义

    我正在使用笑话 酶设置进行测试 如果定义了窗口 我有一个有条件地渲染某些内容的函数 在我的测试套件中 我试图达到第二种情况 即未定义窗口 但我无法强制它 it makes something when window is not defin
  • 如何移除后退堆栈片段的焦点?

    我在我的应用程序中使用片段 我有一个片段包含EditText还有一些Dialogfragment 当我单击一个特定的小部件时 它将移动到下一个片段 我需要后台堆栈中的第一个片段 因此我还添加了 addToBackStack 方法 第二个片段
  • 以图形方式显示 IntelliJ 中 git log --follow 的等效项

    IntelliJ 14 有没有办法显示特定文件的完整日志 我的意思是 执行一种git log follow以图形方式查看旧版本 在这些文件可能被重命名之前 目前 当我这样做时Git gt 显示历史记录在文件上 它仅显示相当于git log
  • 不带任何操作的 asp.net 路由语法

    我正在尝试建立一条没有任何操作而只有一个参数的路线 domain com 不带任何参数 应转到一个控制器 however 域名 com somestring 域名 com anotherstring 域名 com anythingreall
  • 将 uint16_t 转换为 char[2] 以通过套接字发送(unix)

    我知道大致上有关于这方面的事情 但是我的大脑受伤了 我找不到任何东西可以让这项工作发挥作用 我正在尝试通过 unix 套接字发送一个 16 位无符号整数 为此 我需要将 uint16 t 转换为两个字符 然后我需要在连接的另一端读入它们并将
  • 系统设置意图后无法返回活动

    在我的应用程序中 我需要进入手机的设置活动来激活 GPS 并希望使用以下代码返回我的应用程序 Intent intent new Intent Settings ACTION LOCATION SOURCE SETTINGS startAc