I know that I can start activity on boot by calling it from a BroadcastReceiver but what if I wanted to do the following:
<activity
android:name="MyActivity"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</activity>
is this possible? and how?
is this possible?
No.
First, android.intent.action.BOOT_COMPLETED is a broadcast Intent. You cannot respond to it via an <activity> or <service> manifest element.
Second, android:permission="android.permission.RECEIVE_BOOT_COMPLETED will prevent anything from starting your activity, unless it also holds RECEIVE_BOOT_COMPLETED. That is not how you usually use that particular permission.
Related
I bet it's repeated question but I need to ask it again. Service cannot start even I've put following code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastreceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="com.im.HomeActivity"
android:clearTaskOnLaunch="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.im.ListActivity"
android:label="#string/title_activity_list" >
</activity>
<service
android:name="com.im.SyncService"
android:process=":remote" >
</service>
</application>
and
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, SyncService.class);
context.startService(intent);
Log.i("Autostart", "started");
}
}
Help me, please.
Is your BraodcastReciever getting invoked?
if not then the reason could be following:
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.
once the user runs the app for the first time (and does not Force Stop
it), everything behaves as before — a reboot will cause BOOT_COMPLETED
broadcasts to be received and so on. However, if the user installs the
app, until and unless they run the app manually, no broadcasts will be
received.
So in your case you will have to create launcher activity and make sure you start that launcher activity at least once then you will start receive boot event broadcast.
Source
Starting from Android 3.1, a user must start the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
Also android:allowBackup="true" is set in your manifest file, make sure the App is not installed on the SD card. If you are saving to external storage, you will need to setandroid.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE instead.
On some phones(like HTC) there is a Fast Boot option, If it is activated, The BOOT_COMPLETE will not be invoked.
Another approach would be to use Intent.ACTION_SCREEN_ON and check if service is running, if it isn't, then start the service. More info available here
I read loast of tutorials about how to make an app start at system boot (Link, Link...).
My receiver looks like this, the rest like described in the tutorials:
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But my app just won't start... any ideas?
Too stuipid!
You need to add the full path to the receiver class:
<receiver android:enabled="true" android:name="com.mypackage.whatever.BootUpReceiver"
My receiver looks like this, the rest like described in the tutorials
Your <receiver> element is incorrect. You are requiring that the sender of the broadcast hold the RECEIVE_BOOT_COMPLETED permission, which may or may not be true. Please remove the android:permission attribute. If needed, add RECEIVE_BOOT_COMPLETED as a <uses-permission> element, to say that you wish to hold that permission.
Can We only work with one broadcast receiver ?
I have some broadcast receiver and they works well , :
Note: Only one BroadcastReceiver class can be specified per application. Should you need to incorporate two or more BroadcastReceivers from different SDKs, you will need to create your own BroadcastReceiver class that will receive all broadcasts and call the appropriate BroadcastReceivers for each type of Broadcast.
Yes, you may use a single BroadcastReceiver to catch all action strings. Make sure you do add all the action string in your IntentFilter used by that receiver to make it work.
An <application> can contain multiple <receiver> and each <receiver> can contain multiple <intent-filter>. E.g.:
<application>
<receiver android:name="ReceiverA">
<intent-filter>
<action android:name="android.intent.action.ACTION1"/>
</intent-filter>
</receiver>
<receiver android:name="ReceiverB">
<intent-filter>
<action android:name="android.intent.action.ACTION2" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION3" />
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
But you can only have one "com.google.android.apps.analytics.AnalyticsReceiver" - that is IMO what the documentation means.
I have following receiver that listens to Boot_Completed
<receiver android:name=".receivers.ActionBootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
I want to add another intent-filter with my own custom action. And make it private to my app if possible. This is mostly for code reuse so I can run same code path as when BOOT_COMPLETED.
So, I need following (if it's even possible)
1. intent-filter and make it private to my app
2. Code to send that intent so my receiver get's it.
Thanks!
Just use another <action> tag! It is all you need to register receiver for an particular intent.
I have implemented simple message(xmpp protocol) application using smack api.there are number activities which are receiving notification when incoming message comes.now i want to notify my application when the incoming message comes even the application is not running.By reading documentation i know that this is achieved by Broadcast receiver.But how to adopt this to my application.
You don't need the broadcast receiver to do this.
In you androidmanifest.xml for eg: consider this activity.
<activity android:name=".activity.message"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</activity>
Once you add the intent to the activity's intent filter list in android.xml,whenever that intent is thrown your activity will be invoked irrespective of whether it is open or not.
Here the intent you would want to add to listen for incoming messages would be
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
I think you also need to have permissions for this so add this also to your manifest.xml
<uses-permission id="android.permission.RECEIVE_SMS" />