无法返回到活动系统设置后意图 [英] can't return to activity after System settings intent
问题描述
在我applicattion我需要去取消手机设置活动来激活GPS和想用这个code code回到我的应用程序:
意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(意向);
但是当我用它,我不能回到我的应用程序。当我pressed回来,我想这样的:
意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(意向,0);
然后我打电话给所有类型的结果这个code
公共无效的onActivityResult(INT申请code,INT结果code,意图X){
如果(要求code == 0){
如果(结果code == RESULT_CANCELED){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
}
} 如果(结果code == RESULT_FIRST_USER){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
} 如果(结果code == RESULT_OK){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
}
}
}
但是这也没有工作..The同样的事情发生(没有回到我的应用程序)..任何想法? ...在此先感谢
这里是整个活动的code:
公共类Layout2Activity延伸活动{
静态字符串username4;
私有静态最终诠释DIALOG_ALERT = 10;
私有静态最终诠释DIALOG_ALERT2 = 8;
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(event.getAction()== KeyEvent.ACTION_DOWN){
开关(键code){
案例KeyEvent.KEY code_BACK:
的ShowDialog(DIALOG_ALERT); 返回true;
}}
返回super.onKeyDown(键code,事件);
}
布尔isGPSEnabled = FALSE;
布尔isNetworkEnabled = FALSE;
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.menu);
动作条动作条= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(假);
共享preferences名称= getShared preferences(用户,MODE_PRIVATE);
username4 = name.getString(名+ Passwardentry.usernomain,管理); 共享preferences P = getShared preferences(用户,MODE_PRIVATE);
串号= p.getString(暴民+ Passwardentry.usernomain,请不分配。); 共享preferences持续= getShared preferences(用户,MODE_PRIVATE);
共享preferences.Editor编辑= last.edit();
edit.putString(最后一个数字号);
edit.commit(); TextView的K =(的TextView)findViewById(R.id.textView2);
k.setText(username4); TextView的T =(的TextView)findViewById(R.id.textView4);
t.setText(数);
如果(serve.isStarted){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
// TextView的N =(的TextView)findViewById(R.id.textView5);
// n.setTextColor(0x7fff00);列城埃尔2alwan网扎赫拉?
//n.setText(开); }其他{
// TextView的N =(的TextView)findViewById(R.id.textView5);
// n.setTextColor(为0xFF0000);
//n.setText(关);
按钮B =(按钮)findViewById(R.id.button1);
b.setText(关);
b.setTextColor(Color.rgb(139137137));
}} 公共无效userchange(视图v){
意图I =新意图(getApplicationContext(),setuserc.class);
startActivity(ⅰ);} 公共无效servicebutton(查看V){ 如果(serve.isStarted){
// TextView的N =(的TextView)findViewById(R.id.textView5);
// n.setTextColor(为0xFF0000);
//n.setText(关);
按钮B =(按钮)findViewById(R.id.button1);
b.setText(关);
b.setTextColor(Color.rgb(139137137));
意图I =新意图(getApplicationContext(),serve.class);
stopService(ⅰ); }其他{
// TextView的N =(的TextView)findViewById(R.id.textView5);
// n.setTextColor(0x7fff00);
//n.setText(开);
的LocationManager的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE); //获取GPS状态
isGPSEnabled =的LocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER); //获取网络状态
isNetworkEnabled =的LocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 如果(isGPSEnabled&安培;&安培; isNetworkEnabled){ 按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);}
其他{
的ShowDialog(DIALOG_ALERT2); } }} 公共无效readingsbutt(查看V){
意图I =新意图(getApplicationContext(),Readings.class);
startActivity(ⅰ);
} 公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_intro,菜单);
super.onCreateOptionsMenu(菜单);
// INT组1 = 1;
//菜单项infoBtn = menu.add(group1,1,1,关于);
//菜单项MN = menu.add(组1,2,2,ARM);
// mn.setIcon(R.drawable.set);
// infoBtn.setIcon(R.drawable.set); // INT组2 = 2;
//菜单项的appSettings = menu.add(group2,3,2,应用程序设置);
// appSettings.setIcon(R.drawable.set); 返回true;
} 公共布尔onOptionsItemSelected(菜单项项){
如果(item.getItemId()== R.id.menu_settings){
意图I =新意图(getApplicationContext(),settings.class);
startActivity(ⅰ);
overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
}
如果(item.getItemId()== R.id.help){
意图I =新意图(getApplicationContext(),help.class);
startActivity(ⅰ);
}
如果(item.getItemId()== R.id.about){
意图I =新意图(getApplicationContext(),Info.class);
startActivity(ⅰ);
}
//开关(item.getItemId()){
// 情况1:
//突破;
//案例2:
//突破;
//} 返回true;
} @覆盖
保护对话框onCreateDialog(INT ID){
开关(ID){
案例DIALOG_ALERT:
//创建出AlterDialog
建设者建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出?);
builder.setCancelable(真);
builder.setPositiveButton(否,新OkOnClickListener());
builder.setNegativeButton(是,新CancelOnClickListener());
AlertDialog对话框= builder.create();
dialog.show();
案例DIALOG_ALERT2:
//创建出AlterDialog
建设者builder2 =新AlertDialog.Builder(本);
builder2.setMessage(无可用位置服务,请激活网络和GPS卫星);
builder2.setCancelable(真);
builder2.setPositiveButton(取消,新OkOnClickListener2());
builder2.setNegativeButton(设置,新CancelOnClickListener2());
AlertDialog dialog2 = builder2.create();
dialog2.show();
}
返回super.onCreateDialog(ID);
} 私人final类CancelOnClickListener工具
DialogInterface.OnClickListener {
公共无效的onClick(DialogInterface对话,诠释它){ 的setResult(0);
完();
System.exit(0); // /埃尔2aaady
}}
私人final类OkOnClickListener工具
DialogInterface.OnClickListener {
公共无效的onClick(DialogInterface对话,诠释它){ }
} 私人final类CancelOnClickListener2工具
DialogInterface.OnClickListener {
公共无效的onClick(DialogInterface对话,诠释它){
意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(意向,0); }}
私人final类OkOnClickListener2工具
DialogInterface.OnClickListener {
公共无效的onClick(DialogInterface对话,诠释它){}
}公共无效的onActivityResult(INT申请code,INT结果code,意图X){
如果(要求code == 0){
如果(结果code == RESULT_CANCELED){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
}
} 如果(结果code == RESULT_FIRST_USER){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
} 如果(结果code == RESULT_OK){
按钮B =(按钮)findViewById(R.id.button1);
b.setText(开);
b.setTextColor(0xff2d9ede);
意图K =新意图(getApplicationContext(),serve.class);
startService(K);
}
}
}
下面为我工作。
包com.example.test;进口android.os.Bundle;
进口android.provider.Settings;
进口android.app.Activity;
进口android.content.Intent;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类MainActivity延伸活动{
布尔firstTime = TRUE;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
Log.i(测试,的onCreate);
Button按钮=(按钮)findViewById(R.id.button1);
button.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(意向);
}
});
}@覆盖
保护无效onResume(){
super.onResume();
Log.i(测试,onResume);
如果(firstTime){
Log.i(测试,这是第一次);
firstTime = FALSE;
} 其他{
Log.i(测试,这不是第一次了); }}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
}
In my applicattion i need to go to de settings activity of the phone to activate GPS and want to return to my application using this code code:
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent);
but when I used it I couldn't return to my app. when I pressed back so I tried this :
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
then I called all types of result in this code
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);
}
}
}
but this also didn't work ..The same happened (didn't go back to my app).. any Ideas ?? ... thanks in advance
here is the code of the whole activity:
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);
}
}
}
Below works for me.
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;
}
}
这篇关于无法返回到活动系统设置后意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!