In my android application i am using implicit intent for turn on the location service. I want to restart the application after turn on the service. How can i check the result of location service and restart my app.
I am used following code
AlertDialog.Builder dialog = new AlertDialog.Builder(Splash.this);
dialog.setMessage("Location not enabled");
dialog.setPositiveButton("open settings",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface paramDialogInterface,int paramInt)
{
// TODO Auto-generated method stub
Intent myIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
});
dialog.setNegativeButton("cancel",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt)
{
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
I want to restart my activity when i press back button from location service using below code.
Intent myIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
Thanks.
Use startActivityForResult,
#Override
public void onClick(View v)
{
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
}
And check to confirm it in onActivityResult,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Button bt = (Button)findViewById(R.id.turnGPS);
bt.setVisibility(View.INVISIBLE);
this.finish();
}
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Hope it helps.
Note: I Assume that your SplashScreen's launchMode is not singleTask.
Related
I have 3 classes: MainActivity, HighScoreActivity, and GameScreenActivity. I want to go to the GameScreenActivity via MainActivity, and then when the game has finished, pass the score to the HighScoreActivity. Everything gets passed to the GameScreenActivity successfully, but it doesn't go to the onActivityResult in HighScoreActivity.
I believe it's because of the MainActivity having the startActivityForResult, so the GameScreenActivity's finishQuiz() is expecting to return its data to the MainActivity. How would I pass the GameScreenActivity's data to the HighScoreActivity?
MainActivity()
MainActivity(){
...
private void startQuiz() {
String difficulty = spinnerDifficulty.getSelectedItem().toString();
Intent intent = new Intent(getBaseContext(), GameScreenActivity.class);
intent.putExtra(EXTRA_DIFFICULTY, difficulty);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
}
GameScreenActivity()
GameScreenActivity(){
...
private void finishQuiz(){
Intent resultIntent = new Intent(getBaseContext(),
HighScoreActivity.class);
resultIntent.putExtra(EXTRA_SCORE, score);
resultIntent.putExtra(EXTRA_DIFFICULTY, difficulty);
setResult(RESULT_OK, resultIntent);
startActivity(resultIntent);
}
}
HighScoreActivity()
HighScoreActivity(){
...
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_QUIZ){
if (resultCode == RESULT_OK) {
int score = data.getIntExtra(GameScreenActivity.EXTRA_SCORE, 0);
String difficulty = data.getStringExtra(GameScreenActivity.EXTRA_DIFFICULTY);
if (score > highScore10) {
updateHighscore(score, difficulty);
}
}
}
}
}
-------------------------------------------------------------
Trying with the Broadcast method:
GameScreenActivity() with broadcasting methods
GameScreenActivity(){
...
private void finishQuiz(){
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("scoreEvent");
intent.putExtra(EXTRA_DIFFICULTY, difficulty);
intent.putExtra(EXTRA_SCORE, score);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
sendBroadcast(intent);
Intent goToHighScores = new Intent(getBaseContext(), HighScoreActivity.class);
startActivity(goToHighScores);
}
}
HighScoreActivity() with broadcasting methods
HighScoreActivity(){
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("scoreEvent"));
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_QUIZ){
if (resultCode == RESULT_OK) {
int score = data.getIntExtra(GameScreenActivity.EXTRA_SCORE, 0);
String difficulty = data.getStringExtra(GameScreenActivity.EXTRA_DIFFICULTY);
if (score > highScore10) {
updateHighscore(score, difficulty);
}
}
}
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
Considering HighScoreActivity is running,
You can utilize LocalBroadcastManager to register local broadcastReceiver in HighScoreActivity with IntentFilter. Also, don't forget to unregister in onDestroy()
Now, instead of setResult do sendBroadcast() from GameScreenActivity
Hope this helps. Happy coding!
I am using createConfirmDeviceCredentialIntent() for pin authentication in my android application. If I press back button from my pin authentication intent, the createConfirmDeviceCredentialIntent closes and it displays main activity. I want that from that intent if someone presses back button the app should get closed. `KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent i = keyguardManager.createConfirmDeviceCredentialIntent("unlock", "confirm_pattern");
try {
startActivityForResult(i, LOCK_REQUEST_CODE);
} catch (Exception e) {
Log.d("exception occured", e.toString());
}
} #Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case LOCK_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Intent a = new Intent(MainActivity.this, HomeActivity.class);
startActivity(a);
textView.setText("unlock_success");
} else {
textView.setText("unlock_failed");
}
break;
}
}`
So I'm trying to build a QR Code Scanner, but my result only showing a text. Even the QR Code is have a URL link on it. It can't be open to a browser.
Here's my code for the scanner:
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
Button scan_btn = findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}}
What should I add or change to get clickable URL result? Can someone fix this? Please help me to get my project done. I really need it.
You will get QR results on result.getContents() as soon as you get it you should have to use action intent for view to open URL in browser
I have a 2 activity A and B .
In activity A I start activity B . On activity B I want to do photo and go back to activity A and do next step.
In activity A I have :
Intent intent1 = new Intent(this, CameraActivity.class);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent1, REQUEST_CAMERA);
In activity B I have
buttonClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
camera.autoFocus(new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
startActivity();
}
});
}
});
private void startActivity(){
Intent output = new Intent();
output.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
setResult(REQUEST_CAMERA, output);
finish();
}
And on Activity A I have :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE) {
onSelectFromGalleryResult(data);
} else if (requestCode == REQUEST_CAMERA) {
onCaptureImageResult(data);
}
}
}
I don't know how on Activity B put fileUri and start good method on activity A
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new ImageTask().execute(data);
clearCamera();
}
};
Intent output = new Intent();
output.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
setResult(RESULT_OK, output);//change this
finish();
I have an application in which multiple activities are shown in a certain order, which can change based on events in the current activity. I have a "master" activity which manages which activities are shown. Each ativity is started with startActivityForResult() with a requestCode unique to that activity.
When the activity finsishes, I set the resultCode to a value which will have meaning to the master activity. In the master activity's onActivityResult method, I have a switch (requestCode), which will tell me which activity has returned, and in each case block I work with the resultCode to determine which activity to start next.
The problem I'm having, is that at times, seemingly randomly, the application behaves quite erratically, showing activities out of sequence.
I've been unable to replicate the issue while debugging, so all the information I have looks good, but the end users are constantly complaining about the erratic behaviour.
How can I test to see where the problem is?
The code from the master activity's onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case Settings.SCREEN_UPDATE:
ShowActivity(LoginActivity.class, Settings.SCREEN_LOGIN);
break;
case Settings.SCREEN_LOGIN:
if (resultCode == RESULT_OK) {
Settings.CurrentReport = new Report();
Settings.CurrentReport.setUserId(data.getIntExtra("userId", -1));
selectStore();
} else {
finish();
}
break;
case Settings.SCREEN_PRODUCT: // Coming back from the product selection screen
if (resultCode == RESULT_OK) {
ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
} else if (resultCode == RESULT_CANCELED){
ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
}
break;
case Settings.SCREEN_ACTION: // Coming back from the action screen, regardless of result, show options screen.
ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
break;
case Settings.SCREEN_OPTIONS: // All choices return result_ok. Check the "mode" extra
String mode = data.getStringExtra("mode");
processOption(mode);
break;
case Settings.SCREEN_SESSION:
if (resultCode == RESULT_CANCELED) {
ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
} else if (resultCode == RESULT_OK ){
ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
}
break;
} // switch (requestCode)
} // protected void onActivityResult(int requestCode, int resultCode, Intent data) {
private void processOption(String mode) {
if (mode.equals("select")) {
ShowActivity(ProductActivity.class, Settings.SCREEN_PRODUCT);
} else if (mode.equals("repeat")) {
Settings.CurrentReport.repeatItem();
ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
} else if (mode.equals("session")) {
ShowActivity(SessionActivity.class, Settings.SCREEN_SESSION);
} else { // mode equals "end"
confirmFinish();
}
}
private void ShowActivity(Class cls, int requestCode) {
Intent activity = new Intent(this, cls);
startActivityForResult(activity, requestCode);
}
Then, the code from one of the other activities handling the finish() event:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.options);
Button btn;
btn = (Button)findViewById(R.id.btnSelect);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = getIntent();
intent.putExtra("mode", "select");
setResult(RESULT_OK, intent);
finish();
}
});
btn = (Button)findViewById(R.id.btnRepeat);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = getIntent();
intent.putExtra("mode", "repeat");
setResult(RESULT_OK, intent);
finish();
}
});
btn.setEnabled(Settings.CurrentReport.hasPreviousItem());
btn = (Button)findViewById(R.id.btnSession);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = getIntent();
intent.putExtra("mode", "session");
setResult(RESULT_OK, intent);
finish();
}
});
btn = (Button)findViewById(R.id.btnEnd);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = getIntent();
intent.putExtra("mode", "end");
setResult(RESULT_OK, intent);
finish();
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Intent i = getIntent();
i.putExtra("mode", "end");
setResult(RESULT_OK, i);
finish();
break;
}
return super.onKeyDown(keyCode, event);
}
Im just guessing, but maybe Intent intent = getIntent(); is causing the problems. You could try to replace it with Intent intent = new Intent();
getIntent() returns the intent that started the current activity and shouldn't be used as a result.