Is there any event, method, flag or something else which tells me, that the next Activity has been started successfully and is shown on the screen.
MyCode to start an Activity class:
Intent intent = new Intent(this, nextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Thanks in advance
onCreate() is called when the activity is created, I would strongly recommend reading up on the Activity Lifecycle if you're not sure about this.
If you can modify the started activity why not just send an broadcast once new activity's onStart() called?
or do something else you need upon activity start
Related
I am trying to create an application that calls the sender of an SMS as soon as the smartphone receives an SMS.
This is my code:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
// ... (Managing SMS)
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + sender));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
But it dials and calls the sender only when the application is in foreground, while I'd like it to work always. Using the debugger, the execution flows, but it is unable to start the ACTION_CALL activity somehow. Am I missing anything? Thank you very much in advance
Since Android 10, there are limitations on when/how background processes (Service, BroadcastReceiver) can launch activities. This is your problem.
See this guide for more details.
while I'd like it to work always
That is not an option on modern versions of Android. You cannot start an activity from the background, because you do not know what is going on in the foreground at the time. For example, if the user is relying on a navigation app for driving, taking over the foreground could cause the user to crash.
You could raise a high-priority Notification instead.
I broadcast an intent like this
Intent intent= new Intent();
intent.setAction("com.my_app_two");
sendBroadcast(intent);
I only want com.my_app_two to get it.
I tried adding an extra to check but there has to be an easier way.
have you looked into using a Pendingintent instead of a broadcast as suggested in this answer as well ?
send broadcast intent to only one application without using explicit intent
The solution was to use setPackage
There I can specify which package should receive the intent.
I need that my application restarts when click a button, for more info I need simulate thant I push back key and open application again.
I supose that i can close application with finish(); but how can i do to launch onCrete() again?
is there other way to do this?
thanks
Call your Activity using an Intent. It will launch the activity again.
Intent intent = new Intent(MyActivity.this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this will always start your activity as a new task
startActivity(intent);
You can also add, android:launchMode="singleTask" inside your Manifest so that only one instance of your activity will be maintained at any time.
Same thing here. :)
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
To restart your application you can use alarm manager to make your activity open after you exit the app.
AlarmManager alm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(), android.os.Process.SIGNAL_KILL);
I'm currently trying to place a call in the background.
Therefore I call this in my Main-Activity:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
I also wrote an Outgoing Call Receiver which changes the activity:
Intent myIntent = new Intent(context, DisplayCalcActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
The problem is now, that the call-screen is in the foreground. If I switch manually from the call screen to my app, the activity has changed as I wanted, but how is it possible to start the activity in the foreground, so that the call-screen won't be shown at all?
The Android SDK does not allow to make a call in "foreground" maybe you can do accessing to private members but this is not recommended. The only way to call is by dialer app.
Got the solution!
I used another Receiver to listen to "android.intent.action.PHONE_STATE".
The outgoing call, activates the receiver which starts the other activity.
I have an app, where some calls can be made when you press a button.
I call a number with:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+o.getTel()));
startActivity(callIntent);
I have a broadcast receiver that detects end of call.
But this broadcast receiver also receives calls started from other apps (e.g. dialer app).
How can i differentiate calls started from other apps from calls started in mine?
Tnx
Before you call sendBroadcast(intent), add an extra to the intent,
e.g i.putExtra("sender", "my identifier")
Then in the onReceive of the receiver
String encodedType = intent.getStringExtra("sender");
Then you can test for this string.