I am working on a messaging app, it sends user notification when he is on a different activtyon my app or is on another app but if the user is on MessagingActivity.java it just updates the chat history and does not send any notifications which is perfectly fine, but the problem arises when the user is on MessagingActivity.java meanwhile an email or something else happen user leaves the MessagingActivity.java open and checks that app if in the meantime a message comes user does not receive any notifications
public void parseRequest(Bundle extras) {
if (extras.containsKey("for") && extras.containsKey("recipientID")) {
if (Integer.parseInt(extras.getString("recipientID")) == M.getID(this)) {
switch (extras.getString("for")) {
case "chat":
if (isRunning("MessagingActivity")) {
Intent intent = new Intent("update_messages_list");
intent.putExtra("data", extras);
sendBroadcast(intent);
} else {
Intent resultIntent = new Intent(this, MessagingActivity.class);
resultIntent.putExtra("conversationID", Integer.parseInt(extras.getString("conversationID")));
resultIntent.putExtra("recipientID", Integer.parseInt(extras.getString("ownerID")));
M.showNotification(getApplicationContext(), resultIntent,
extras.getString("ownerUsername"),
extras.getString("message"),
Integer.parseInt(extras.getString("conversationID")));
}
Let me know how you are checking that your MessageActivity is Running i.e. functioning of isRunning("MessagingActivity") method. If you are setting any global boolean variable for checking this and making isRunning value false in onDestroy() method of that activity then, according to life cycle of Activity it is not called until your activity is finished i.e. in your case user just switching from MessageActivity to Mail .
I am by no means an expert, but you could just set a boolean variable by overriding the Activity's onPause() and onResume() events.
Simply set msgActivityActive to true in onResume(), false in onPause(), and change your call to:
if (isRunning("MessagingActivity") && msgActivityActive)
Related
I have implemented Sign In the method using Google Accounts, my problem is the logout function doesn't behave what I want. I don't receive any error, it's just the code doesn't work properly.
Can someone tell me what's wrong with this code?
Options Fragment:
Preference logoutPreference = findPreference(getString(R.string.pref_key_logout));
logoutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
FirebaseAuth.getInstance().signOut();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.example.budgetapp.ACTION_LOGOUT");
getActivity().sendBroadcast(broadcastIntent);
getActivity().startActivity(new Intent(getActivity(), SignInActivity.class));
getActivity().finish();
return true;
}
});
Main Activity:
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.budgetapp.ACTION_LOGOUT");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver(receiver, intentFilter);
}
Why is the code not working? I'm using Firebase Realtime Database
Whenever I click Logout the app redirects me to Sign In Activity (which is good) but when I try to Sign In, it automatically signs in to the previous Google Account.
When you are using the following line of code:
FirebaseAuth.getInstance().signOut();
It means that you are signing out only from Firebase.
Whenever I click Logout the app redirects me to Sign In Activity (which is good) but when I try to Sign In, it automatically signs in to the previous Google Account.
As I understand, you are using the Google Provider for authentication. Signing out from Firebase doesn't mean that you are automatically signed out from Google. To sign out from Google you have to explicitly add a call to GoogleSignInClient#signOut() method:
googleSignInClient.signOut();
Don't also forget that the sign-out operation is asynchronous, meaning that you have to wait until the operation completes. Since this method returns an object of type Task<Void>, you can use addOnCompleteListener(OnCompleteListener listener) method, to know when you are completely signed out.
I have an activity for showing tour details with two inconsistent goals
First
that activity has a booking button which redirects to bank pay and get back to activity after successful or unsuccessful pay.that is why i set Launch Mode in manifest to stop activity from re creating.
android:launchMode="singleTask"
Second
that activity has a button which redirect to similar tour,then i have to call finish(); before startActivity() to make intent work!
onNewIntent() inside activity to get data of first part
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getData() != null) {
Helper.logDebug("fsfsfsgsgsgs", "inside get data not null");
String query = intent.getData().getQuery();
Helper.logDebug("fsfsfsgsgsgs","query is "+ query);
if (query!=null && query.contains("Status=OK")) {
if (frgBtmReserve!=null){
frgBtmReserve.dismiss();
}
String count=query.substring(16);
Helper.logDebug("fsfsfsgsgsgs","count is "+ count);
frgObjectInfo.updateReserveCount(count);
Helper.logDebug("fsfsfsgsgsgs", "inside status ok");
Helper.notifyUserDone(getResources().getString(R.string.success_tour_reserve), this,R.drawable.ic_tick);
} else {
frgBtmReserve.dismiss();
Helper.logDebug("fsfsfsgsgsgs", "inside status nok");
Helper.notifyUserWarning(getResources().getString(R.string.error_tour_reserve), this);
}
}
}
on click of second part which should create new instance of current activity but it doesnt because of launch mode of activity which is singleTask.intent doesnt work until i finish() before startActivity()
Intent intent = new Intent(context, ActivityShowObject.class);
intent.putExtra(ActivityShowObject.INTENT_KEY_TYPE, Obj.TYPE_TRAVEL);
intent.putExtra(ActivityShowObject.INTENT_KEY_COLOR, color);
intent.putExtra(ActivityShowObject.INTENT_KEY_OBJ_ID, obj.getAgencyId());
((AppCompatActivity)context).finish();
startActivity(intent);
Animatoo.animateShrink(context);
that is my problem
think of a user which is looking into some similar tours then press back and app goes back to the very first step! do you guys have a suggestion for me?
Get rid of
android:launchMode="singleTask"
for a start. I think its the wrong usecase, making it harder for you.
You have two scenarios:
1) New activity pay for this tour
2) New activity Browse another tour
for #2 use the normal startActivity route, this will allow normal back navigation
for #1 you could also use normal startActivity but I have a feeling that when they pay successfully you do not want them to press back to go back to the tour, but if they are unsuccessful at paying you do want to allow them to go back?
If that is the case, you can use startActivityForResult when navigating to your #1 pay scenario.
When they complete payment successfully call
setResult(RESULT_OK)
if they are unsuccessful
setResult(RESULT_CANCELLED)
Then when they hit back, the result is propogated to the first activity, and you an use this to either call finish() or not.
I am working with some Estimote beacons (basically bluetooth proximity beacons) and I am creating an android app that switches screens once it picks up a certain beacon.
public class MainActivity extends AppCompatActivity implements hold_undo_fragment.toggleButtonListener {
public void runBeaconDistances(){
final Intent i = new Intent(this, corsi.class);
final Intent j = new Intent(this, search_key.class);
final Intent k = new Intent(this, corsi2.class);
beaconDistances = new BeaconDistances(this, beaconIDs);
beaconDistances.setListener(new BeaconDistances.Listener() {
#Override
public void onBeaconDistancesChanged(BeaconID beaconID) {
if(beaconID != null){
int beaconMinor = beaconID.getMinor();
switch (beaconMinor) {
case 9911: startActivity(i);
break;
case 11904: startActivity(j);
break;
case 27710: startActivity(k);
break;
}
}
}
});
}
}
My onCreate just runs this program, onResume starts looking for beacons, and onPause stops looking.
What I am wondering is if there is a way to extend/pass/run this method in another activity without having to copy it all down again? Or is there a way to have this constantly running in the background and changing activities.
Right now once MainActivity switch to corsi, search_key, or corsi2 it just stays on that activity. Corse, search_key, and corsi2 are empty new activities right now.
Also all beaconDistances does is scan for beacons and returns the closest beacon along with an identification number for that beacon (a minor).
I think what you're looking for is a Service. A Service is what let's you perform longer-running operations that don't require user interaction. You can start your service from your MainActivity and then connect to it from anywhere else that you need to get state from it.
I have an app which plays audio. The thing is, when I get a call on my phone while I am interacting with the app, the audio continues to play. This becomes a problem because the audio makes it hard to understand what the person is saying. Is there any method that gets called when the user is talking on a call? That way I can mute my audio there. I have thoroughly researched the activity life cycle, but cannot find anything that will help me with this issue. I really appreciate all of your expert advice.
I have tried to put this code into my BaseActivity which gets extended by all other activities:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
TwentySeconds.stopTimer();
Intent i = new Intent(BaseActivity.this, FinishedBeforeTimer.class);
startActivity(i);
Toast.makeText(BaseActivity.this, "INCOMING CALL", Toast.LENGTH_SHORT).show();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
TwentySeconds.stopTimer();
Intent i = new Intent(BaseActivity.this, FinishedBeforeTimer.class);
startActivity(i);
Toast.makeText(BaseActivity.this, "DIALING", Toast.LENGTH_SHORT).show();
}
But then the strangest thing happens. When I call the my phone, the phone I am calling with says that I can't call my phone. I am confused as to what to do now, I really appreciate your time.
Thanks,
Rich
i make a lock screen application.. i have some service that listen to the SMS.. when the SMS receiving command andro-lock then i display a lock screen that called by service:
here'e the service's code :
if (tempMessage[0].toString().equalsIgnoreCase("andro-lock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-lock", Toast.LENGTH_LONG).show();
openDatabase();
updateStatusL();
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
}
that code has works perfecly, but i have some problem when i try to unlock my phone.. if my service receive command andro-unlock then i must close (finish) that lockscreen.java.. i was trying many code and still not works.. what must i do to close the lockscreen activity when my service receiving command andro-unlock?? please help..
else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//what must i do??
//any solution??
}
Thanks for your help..
A possible solution to stop the activity would be:
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);
In LockScreenForm
onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
finish();
}
You could send an intent to your activity with an extra, and the activity could read this extra and, if present, finish() itself.
Regards,
Stéphane