How can I start few activities from service class? - android

I am trying to make app that implements service for running apps (eg. yt, chrome, google maps) and measuring internet connection and how much data is downloaded.
So the idea is to start activities and run applications metioned above in patterns, eg.
Run some YT video for 5 mins.
Load 10 articles in Chrome one after another.
...
And every one min internet connection statistics are gathered.
The issue is that when I do
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
inside service the startActivity works only once, every other time debug console shows:
I/Timeline: Timeline: Activity_launch_request time:29210990 and no avtivity is started.
I have tried bunch of different flags on intent:
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Not sure how it's suppose to be done, thanks for any help :)

Related

How to Start Activity From Service Android Q

Hi Developers I hope All are You fine. I need help. I make a AppLocker app and I use service to check which app i currently running. i locked app in my database with package name. When I show my lock screen From Service my Intent hit but lockScreen not Showing.How i call Activity From Service below is my Codeto start Activty from Service
if (locker_list!=null) {
if (launchapp) {
Log.d("TAG", "run: lock Screen Show");
Intent intent = new Intent(mContext, Lock_app_screen.class);
intent.putExtra("package_name", current_app);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
for more...
see this

How to start activity from service in Android10

I was starting activity from services till android P, but from android10 google has kept one restriction that activity cannot be started from background.
https://developer.android.com/guide/components/activities/background-starts
// below code stopped working
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
what should i do for android10 ?
You can use a notification using setFullScreenIntent, it's the best you can do or you can ask for SYSTEM_ALERT_WINDOW permission but it doesn't work with android go devices.

How can I auto install programmatically app without start Intent.FLAG_ACTIVITY_NEW_TASK

In fact, I want to be noticed when the program is downloaded without the user having to install their own apps (Install without show permissions activity).
Now I use the following code, and I want to change this code to answer my question.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
The following image is displayed after downloading the app from my server and i dont want to show

Bring the app to foreground and if it is already running rather than creating new instance of it in android

In my app I want to launch apps(messaging,contacts,etc)if my launched app(messaging,contacts,etc) is already running in background I want it to bring front.I tried using moveTaskToFront() but it doesn't implement from above API 23(Lollipop).So,I ended up with this code:
Intent intent=getPackageManager().getLaunchIntentForPackage("com.android.mms");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
By using this code if I launch a messaging from inside my app it launches,and if I again launches the same app from inside my app it perfectly resumes it and continue from where we left it.
But when I Launches the same messaging app from my default android launcher it just create new instance of messaging app on above of my already running messaging app which was already launched by my app.
I don't know what the solution for this.Please help me ...
Use the below approach to invoke intents within your app like this.
Intent intent = new Intent(CONTEXT, TARGET_ACTIVITY.CLASS);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Android - how to open an audio-stream url with an external app?

I want to give the user the choice of opening an audio stream url with an audio player that he already have installed.
The following code works as so far that the user getting a list of installed audio apps and can choose between then.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(stream.getAudio()), "audio/*");
startActivity(intent);
But it will open an intent that lives inside my app only. Therefor the playback will stop if the user closes my app. How can I change the behaviour so that not an new activity is started but the whole app is launched and is detached from my app.
I guess it's because the audio player is opened in the same task stack of your app.
Try Intent.FLAG_ACTIVITY_NEW_TASK to start it as a new task, like:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
See Tasks and Back Stack to learn more.

Categories

Resources