i have a project and when i run the application,one service should be active and when the device is turned on,my android service should be active.
The project runs in emulator successfully but in my phone when i turn on the device it doesn't works!
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You are registering a BroadcastReceiver not a Service. Services will generally run in the background all of the time (if memory and resources permit).
Your broadcast receiver activates only when the device is rebooted - and your emulator "boots" every time you start it up. Your phone does not "boot" when the app is installed.
You should either register for events like "app installed" or else actually implement a service, like here:
http://www.vogella.com/tutorials/AndroidServices/article.html
You need to make sure that once you install the app on device it should be started once by clicking app icon then only boot receiver will receive boot event on subsequent reboots calls.
i undrestand that if i install app in internal storage in works successfully even device rebooted but when i install the app on external storage and then reboot device it not work
i have a project that when run application one service be active and when device be turn on my android service be active. the project run in device's internal memory successfully but when i install it on external memory and reboot my device again don't work!
for first i call my service in first acitivity and it works, but when i reboot my device it doesn't work !
my activity :
public class FirstClass extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
startService(new Intent(getApplicationContext(), MyService.class));
startActivity(new Intent(FirstClass.this, MainActivity.class));
finish();
}
},5000);
}
/////////////////////////////////////////////////
}
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<service
android:name="com.dariran.MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
i added this code to my service class to put a filter to recognize the external storage but don't work again :(
#Override
public void onStart(Intent intent, int startId) {
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
BroadcastReceiver mReceiver = new BroadcastReceiverOnTurnedOn();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
}
Related
I have a project which is only a service and it has no activity and user interface. I want to start my application background service when phone boot completely. but I never receive the "BOOT_COMPLETED" Message from OS. these are my code:
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
</application>
<service android:name=".mySevice"></service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
Broadcast Receiver:
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("boot Received", intent.getAction());
Intent serviceLuncher = new Intent(context, myService.class);
context.startService(serviceLuncher);
}
}
myService:
public class LocationNotifierService extends Service {
Timer timer ;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate(){
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
}
},3000);
}
#Override
public void onDestroy(){
}
#Override
public int onStartCommand(Intent intent, int flagId, int startId){
return START_STICKY;
}
}
but I never get "boot Received" log.
is there any mistake and is there any way to debug my program?
I Recommend that my project must have only this Service and it cannot have any UI.
I never receive the "BOOT_COMPLETED" Message from OS
Partly, that is because you do not have a <receiver> set up to receive android.intent.action.BOOT_COMPLETED broadcasts.
Partly, that is because your app will not receive broadcasts until something on the device uses an explicit Intent to start one of your components. The way your app is set up — without an activity that the user can run — it is unlikely that any app will do this, and so your code will never run.
Also, please bear in mind that Android O has changes designed specifically to prevent background services from running for very long and to limit your ability to get background location updates (which your location_tracker name suggests that you want to add in the future). You may wish to reconsider whether writing this app the way that you are is a wise course.
try this in your manifest
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have a broadcast which monitors for unlock event for the phone. But when the app's process is killed and no longer in memory, Unlocking the phone does not trigger the Receiver, instead I can see in the Android studio, that new process is created for that app.
If lock and unlock it again, then as the process is already running, I can see the BroadcastReceiver is triggered.
<receiver
android:name=".UserPresentBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UserPresentBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = UserPresentBroadcastReceiver.class.getSimpleName();
#Override
public void onReceive(Context arg0, Intent intent) {
Log.d(TAG, "onReceive: Unlock Boradcast received");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Toast.makeText(arg0, "You just unlocked the device", Toast.LENGTH_LONG).show();
}
}
}
I am unable to understand this behavior. Is this the default behavior?
You have to register and unregister this broadcast receiver in Activity(or Service for listening in background all the time).
Manifest entry won't work.
I need my app to start running (in the background) after rebooting the device. Here's what I've come up with till now (after taking a lot of help from here...)
This is my BootUpReceiver making use of broadcastreceiver:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, RebootService.class);
serviceIntent.putExtra("caller", "RebootReceiver");
context.startService(serviceIntent);
}
}
This is the service class:
public class RebootService extends IntentService{
public RebootService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
protected void onHandleIntent(Intent intent) {
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intentType = intent.getExtras().getString("caller");
if(intentType == null)
return;
if(intentType.equals("RebootReceiver"))
getApplication().startActivity(i);
}
}
THis is my android manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".RebootService"/>
</application>
The problem is, When I install this on my phone and reboot, the app crashes: It says, "Transfer has stopped working". After I press the OK button, when I check the app info, the app is running.
I'm new to android and I'm not sure what is going on. Am I supposed to add any more permissions?
Kindly help.
TIA
I think your problem lies with your RebootService constructor. When the system invokes it, it doesn't provide any arguments, so it's going to crash. If you look in the logs you'll probably see something to the effect of "Unable to instantiate service..."
Try replacing your constructor with:
public RebootService() {
super( "Reboot Service" );
}
I've read some tutorial on launch service on boot.
What I've done is:
In manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<receiver android:name="my.package.ServiceStartup" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
CODE:
public class ServiceStartup extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent dialogIntent = new Intent(getBaseContext(), MyActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
}
}, 10000);
}
}
In this way, if i reboot my device and go to setting in active applications, my service is not launched. What can I do? Where I make error? thanks!!
You want to start activity or service. In case of service, you will have to call startService(). Like:
getApplication().startService(new Intent(this, MyService.class));
Did you run your app? Refer to this tutorial
If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
I want to put up a notification in the notification bar that will launch my app when pressed. While I have no problems doing this, my users want the notification to come up after a reboot as well. They have an app from another vendor that does this.
Everything I can find states that the app must be running for the notification to display. Any ideas?
You need to add a receiver that launches a Service after a reboot.
In your manifest register for Boot Complete
<service android:name="com.meCorp.service.MeCorpServiceClass"/>
...
<receiver android:name="com.meCorp.receiver.MyRebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
In your boot receiver, launch a service.
public class MyRebootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, MeCorpServiceClass.class);
serviceIntent.putExtra("caller", "RebootReceiver");
context.startService(serviceIntent);
}
}
Here is an example for a service class to run in the background.
public class MeCorpServiceClass extends IntentService{
#Override
protected void onHandleIntent(Intent intent){
String intentType = intent.getExtras().getString("caller");
if(intentType == null) return;
if(intentType.Equals("RebootReceiver"))
//Do reboot stuff
//handle other types of callers, like a notification.
}
}
OR Just use a third party like Urban AirShip, which handles all that for you.