OnActivityResult for 2 different activites - android

I am trying to initialize/ turn on the NFC and the BT modules in same activity.
I need them both to be enabled before I continue the task.
I do understand that OnResultActivity is Async so I am trying to figure out what would be the best way to achieve it?
Here's most of the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBT();
initNFC();
Intent nextIntent;
if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
nextIntent = new Intent(MainActivity.this, LoginActivity.class);
}
else
{
nextIntent = new Intent(MainActivity.this, MainMenuActivity.class);
}
startActivity(nextIntent);
finish();
}
private void initBT() {
if(BTModule.GetInstance().initBT().equals(Constants.eBluetoothStatus.BT_DISABLED)){
Intent enableBtIntent = new Intent(BTModule.GetAdapter().ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void initNFC(){
mNFCState = NFCModule.initNFC(this);
if(mNFCState == eNFCStatus.NFC_NOT_SUPPORTED){
Toast.makeText(this, "NFC is not suppoeted for this device", Toast.LENGTH_LONG).show();
}
else if(mNFCState == eNFCStatus.NFC_DISABLED){
Intent nfcIntent;
if(android.os.Build.VERSION.SDK_INT >= 16){
nfcIntent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
}
else{
nfcIntent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}
startActivity(nfcIntent);
Toast.makeText(this, "Please enable NFC", Toast.LENGTH_LONG);
}
else{
Toast.makeText(this, "NFC is up and running", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == eBluetoothStatus.BT_OK.ordinal()){
Toast.makeText(this, "BT is up and running", Toast.LENGTH_LONG).show();
return;
}
}
}
As I am a newbie please feel free to correct me if I am wrong with anything :)
thanks!!

Use startActivityForResult instead of startActivity in initNFC method. Also define 2 boolean variables like isNfcEnabled and isBtEnabled. In onActivityResult method check those booleans. If both of them are enable, do whatever you want.

Related

onActivityResult() not working on Android

I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?
code for onActivityResult in mainActivity.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("There is something coming to this function" + requestCode);
if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
mTitleViewModel.insert(title);
}else{
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
}
}
code in my notesSection activity.
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// System.out.println("Button Has Been Clicked From Notes Section");
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
// System.out.println("Empty?");
setResult(RESULT_CANCELED, replyIntent);
}else{
// System.out.println("Sending Something Supposedly");
String title = titleEdit.getText().toString();
// System.out.println("Sending " + title);
replyIntent.putExtra(EXTRA_REPLY, title);
setResult(RESULT_OK, replyIntent);
}
finish();
// startActivity(new Intent(notesSection.this, MainActivity.class));
}
});
FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary.
Thanks.
In your first activity try starting second activity like this
static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
---
---
---
// i assume you are starting activity on some button click
// so add following line in you button on click event
startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
}
then override following method in your FIRST ACTIVITY
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
{
// do whatever you want
}
}
Also update your finish fab on click listener
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
setResult(RESULT_CANCELED, replyIntent);
}
else{
replyIntent.putExtra("TITLE", titleEdit.getText().toString());
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
Actually, this procedure has changed recently. You might want to take a look at the official documentation so that you can implement it according to your needs.

Showing a service along with specific view

I need to show a floating icon only along with a started maps activity and stop it when the map activity is gone.(when the user navegates back from the maps to my app) floating service over maps
mIntent = new Intent(MainActivity.this, FloatingIconService.class);
public void startTrip(String s , String d)
{
Uri gmIntentUri = Uri.parse("https://www.google.co.in/maps/dir/" + s + "/" + d);
Intent mapIntent = new Intent(Intent.ACTION_VIEW,gmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(mapIntent,4000);
//view floating notes icon
startService(mIntent);
}
onPause is necessary to keep the service runnig along with maps and onActivityResult doesnot do what I want
#Override
protected void onPause() {
super.onPause();
// To prevent starting the service if the required permission is NOT granted.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(this)) {
startService(new Intent(MainActivity.this, FloatingIconService.class).putExtra("activity_background", true));
finish();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DRAW_OVER_OTHER_APP_PERMISSION) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "Premission not granted", Toast.LENGTH_SHORT).show();
finish();
}
}
} else if (requestCode==4000 && (resultCode == Activity.RESULT_CANCELED || resultCode == Activity.RESULT_OK)){
stopService(mIntent);
}else {
super.onActivityResult(requestCode, resultCode, data);
}
}
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startTrip("USA", "Alexandria");
}
});

How to open a system settings activity?

I am trying to open this class on the click of a button, it works fine, however, this class is not present in HTC devices, so i want my app to show a toast on this exception, but it doesn't show anything-
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "you are offline", Toast.LENGTH_LONG).show();
// No equalizer found :(
}
You can open with
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
You can return by pressing back button on device.
Try this
In your MainActivity.java put
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
// here is the changes
// REQUEST_CODE is an any integer value
startActivityForResult(intent, REQUEST_CODE);
} else {
Toast.makeText(getBaseContext(), "Unable to open setting..", Toast.LENGTH_LONG).show();
}
If you want to handle result of an intent then
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// add your code here
}

Android. on button click, jump to an onActivity result function. IT IS POSSIBLE?

i have button which have attribute android:onClick="atnDuom".
There is that function
public void atnDuom(View view)
{
finish();
}
and there is onActivityResult function in the same activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
DOP = new DatabaseOperations(ctx);
Intent returnIntent = new Intent();
user_name = data.getStringExtra("tarpVard");
user_lastname = data.getStringExtra("tarpPav");
institucijos_pavadinimas = data.getStringExtra("tarpInst");
padalinio_pavadinimas = data.getStringExtra("tarpPad");
pareigos = data.getStringExtra("tarpPar");
mob_tel = data.getStringExtra("tarpMob");
el_pastas = data.getStringExtra("tarpEl");
setResult(RESULT_OK,returnIntent);
DOP = new DatabaseOperations(ctx);
if(newVard.equals("")||newPav.equals("")||newInst.equals("")||newPad.equals("")||newPar.equals("")||newMob.equals("")||newEl.equals(""))
{
Toast.makeText(getBaseContext(), R.string.prashome, Toast.LENGTH_LONG).show();
}
else
{
DOP.updateUserInfo(DOP, user_name, user_lastname, institucijos_pavadinimas, padalinio_pavadinimas, pareigos, mob_tel, el_pastas, newVard, newPav, newInst, newPad, newPar, newMob, newEl);
Toast.makeText(getBaseContext(), "Duomenys atnaujinti", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
It is possible to execute function onActivityResult whithout doing anything in atnDuom function?
Finish() close activity and onActivityResult doesnt work :)
You are using data from the intent, if you want to go to onActivityResult from atnDuom you will need to create a new Intent and push all the data needed
Intent newIntent = new Intent();
newIntent.putExtras(...);
onActivityResult(REQUEST_CODE, RESULT_OK, newIntent);

startActivityForResult doesnt return to onActivityResult in android

The first time I call startActivityForResult() with requestCode x it return to onActivityResult().But the second time I call startActivityForResult() within the same activity with diffrent requestCode it doesnt return to the onActivityResult() and just proceed
with the code.
I have tried to add this property to the manifest andter the activity
android:noHistory
Why it doesnt return to onActivityResult()?And how i can fix it?
Thanks a lot,
Leon
Edit: Here is my code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
else if(RESULT_SPEECH == requestCode)
{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textFromSpeech = text.get(0);
}
}
}
private void getTextFromSpeech()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tts_);
/*
* New Intent purely for the purposes of checking the user data
*/
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}

Categories

Resources