im new to android, i made an application and i want it to run at start up automatically
and in the background for sure, can anybody help me with this????
regards
for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int delay = 10000;// in ms
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
AudioManager audio=((AudioManager) getSystemService(AUDIO_SERVICE));
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}, delay);
}
i want this code to run at startup and in background
Listen for this broadcast Intent with a BroadcastReceiver and tell the system about it with a Android Manifest receiver.
If you want anything run in background then you should take the help of services not activity.
Related
I want a best consistent solution to call an api to update current location in every 2 minutes on Nougat and higher version. The process should not be terminated even when the app is killed or closed.
Thanks in advance
Create a services:
public class MyServices extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
startService(new Intent(this,MyServices.class));
Timer t = new Timer();
final Handler handler = new Handler();
// Timer task makes your service will repeat after every 20 Sec.
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
//Do network call here
}
});
}
};
//Starts after 20 sec and will repeat on every 20 sec of time interval.
t.schedule(doAsynchronousTask, 3000,3000); // 20 sec timer
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
}
Register the service in menifest
<service android:name=".MyServices" />
Start the service in your activity
Intent intent = new Intent(this, MyServices.class);
startService(intent);
if version > N use this
startForegroundService(intent);
Create a service and update from there.
Service will not stop after closing the application but it will get stopped if the application is force stopped.
And also if your app goes to doze mode your app cannot use Internet or GPS service from the background.
You should check out WorkManager to schedule any kind of work you want your app to do.
How to run multiple services in background with same interval of time in android? . I tried with AlarmManager but in this it is not running with same intervals like every 5 mins(Sometimes its running correctly but not all the times). Please suggest me best way to achieve this. Thanks in advance.
To run the multiple services in particular interval, you can use timertask
Timer timer =new Timer();
TimerTask timerTask= new TimerTask() {
public void run() {
//TODO
}
};
timer.schedule(timerTask, 1000, 1000);
Example for Service,
public class ServcieSample extends Servcie{
public void onCreate(){
}
}
Inside onCreate you can create any number of threads or asynctask for background operations.
I have an application that i want to set session to application means when i logged in to app and not used for 30 minutes like app running in background or screen off then i want to directly show login screen.is there any solution.?
For managing the session you you have to store the data some where like SharedPrefrence. After that youcan use Handler as given below in your First Activity.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Clear the Shared prefrence here
//here you can start your Login Activity
}
}, 1000);// Change the time according to need
App running in background
First thing you need to start a thread from your application to check the currently running application, find out the application on top use the following code to do this
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
String currentRunningActivityPackageName = taskInfo.get(0).topActivity.getPackageName();
This will return the current running application package, compare it with your application package name. If the application is not matching start a timer, if the timer cross 30 minutes,you can log out of your application, if in mean time your application comes in foreground stop the timer.
Screen off
For screen of you can register a broadcast
BroadcastReceiver mybroadcast = new BroadcastReceiver() {
//When Event is published, onReceive method is called
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.i("[BroadcastReceiver]", "Screen ON");
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.i("[BroadcastReceiver]", "Screen OFF");
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
So according to the intent received you can toggle the same timer.
is it possible to use a timerTask,like:
timer = new Timer();
task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
new AsyncTask().execute();
}
});
}
};
timer.schedule(task, 0, 4000);
I need to start a periodically task when activity is on background ,instead of using a Service, when activity is onPause() and to prevent to be killed by the system?
thanks in advance
You can start your TimerTask and inside it AsyncTast this way. They will run as long as your application process is running, but they will not prevent killing of your Activity and Application. When considering which activity or process to kill android does not take into account threads or timers running inside your app, only Activities, Services, BroadcastReceivers etc count.
im new to android
i have an application that running in the background
using moveTaskToBack(true); method
the problem is if user clicked on the application icon
it will run another process, not just bring the running one to foreground
how can i solve this???
Example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int delay = 10000;// in ms
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
AudioManager audio = ((AudioManager) getSystemService(AUDIO_SERVICE));
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
moveTaskToBack(true);
}, delay);
}
i tried this
<activity android:name=".LaunchActivity" android:label="#string/app_name"
android:launchMode="singleInstance"
>
lanchmode=singleinstance
but it didnt work!!
any help?
To make the activity not have multiple instances use the activity launchMode parameter in your Manifest. Set it to singleInstance or singleTask.