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