Start app on device boot, but not run service - android

I have a BroadcastReceiver that is using the BOOT_COMPLETED intent-filter in the AndroidManifest, which then runs my Service when the device boots. Is it possible to have my Service start, but not actually run the code in the Service? I have an AlarmManager that run the Service at a regular interval, but, ideally, I'd like that code to not run when the device starts.
<receiver android:name="com.app.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Yes, call startActivity:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}

Related

how to make my service work when user open the mobile

I want to make my service always working but in the normal service if the user close the phone and open it or restart it the service is stoping can you help me. thanks
You could use a Broadcast Receiver that would take the permission to broadcast a message on the restart of the phone that would tell the service to be started or as we call it in technical terms we would use an intent-filter having the action of starting the service when in the actions (or the ) the boot process is completed.
In manifest file :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In application tag of manifest.xml :-
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java :-
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
Now, at the end the service class of MyService would be started by this boradcasting.

BroadcastReceiver doesn't work

I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));

android services life time

My question is.
In Android is there a way to create a service which can stay alive even you restart the mobile phone, until it does not perform its task,
For example alarm application. If you restart your mobile it will be triggered without any problem.
In android All services behave like this or do we have some solution for that?
Kindly explain in detail.
It will take 2 steps:
(1) First Create a BroadcastReceiver and start the service in this receiver's onReceive() method:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, YourService.class);
context.startService(service);
}
}
(2) Now decalre this receiver in manifest like this:
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This way your Service will always be running and will start even if the phone is restarted. For more details , refer to this link:
http://www.vogella.com/articles/AndroidServices/article.html
is there a way to create a service which can stay alive even you
restart the mobile phone
No. All running services are killed when turning off a phone.
For example alarm application. If you restart your mobile it will be
triggered without any problem.
Yes, but it's not because the service stayed alive. It's because the alarm app respond to the android.intent.action.BOOT_COMPLETED intent.
What you can do is creating a BoradcastReceiver that responds to this intent and that start your service.
The problem is that the user can kill this service manually. If you want to build an alarm clock, you should not have a service always running in the background.
You should make use of the AlarmManager and PendingIntent.
Something like in your manifest :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".broadcasts.InitReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
And something like this for the broadcast.
public class InitReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Schedule your alarms again.
}
}
And you should schedule alarms like this :
Intent intent = new Intent(context, Ring.class);
intent.setData(alarmUri);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, ringTime, pendingIntent );
Where Ring is the broadcast that handles the alarm ring.
In Android is there a way to create a service which can stay alive even you restart the mobile phone, until it does not perform its task,
No, if we interpret your request literally. All processes are terminated when you restart the device, just as all processes are terminated when you turn off the device.
For example alarm application. If you restart your mobile it will be triggered without any problem.
That is because the "alarm application" is getting control at boot time, via an ACTION_BOOT_COMPLETED BroadcastReceiver, and is rescheduling its alarm with AlarmManager.
Similarly, a service implementing a download queue would get control at boot time, via an ACTION_BOOT_COMPLETED BroadcastReceiver, at which time it would determine what yet needs to be downloaded and return to that work.
Check out the Services documentation. You want the START_STICKY flag which will make the system restart your service if it is ever terminated due to low memory etc.
To handle device restart...
Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".BootService"
android:label="Boot Service" >
<intent-filter>
<action android:name="com.my.package.BootService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.BootReceiver"
android:enabled="true"
android:exported="true"
android:label="Boot Receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Source
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(this.getClass().getName(), "Boot Receiver onReceive()");
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
// Fire up your service.
Intent serviceIntent = new Intent("com.my.package.BootService");
serviceIntent.putExtra(Constants.BOOT_LAUNCH_EXTRA, true);
context.startService(serviceIntent);
}
}
}
public class BootService extends IntentService {
public BootService()
{
super("BootService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d(this.getClass().getName(), "Boot Service onHandleIntent()");
if(intent.hasExtra(Constants.BOOT_LAUNCH_EXTRA))
{
//...
}
}
}

Starting my android application automatically after connecting the USB cable.

Is it possible me to start my android application if the user connecting USB cable to the device ?. I am going through the this link . Am I in the correct path ?
Register an Receiver for ACTION_POWER_CONNECTED in Manifest as:
<receiver android:name=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
and in Code part
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Your_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Yes, you can do it, you need to code for a BroadcastReceiver that will get fire when any event occurs at USB port ( plug-in or plug-out ).
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
when such things occurs, just fire an Intent to start your activity.

Start application in background on bootup

I would like to start my application when the phone boots up. But the user should not know that the application has started. I am doing the boot up by receiving the BOOT_COMPLETE event in a broadcast receiver and am able to minimize the application by using moveTaskToBack(true).
But my application is visible for a split second before it gets minimized and I do not want this to happen. My application does not have any services. Is there any way in which I can start the application minimized without the end user noticing ?
Thanks
enter code here
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

Categories

Resources