Invoking an action of an Intent Service - android

I just created a new intentService app in Android Studio and i'm a bit confused as to how to invoke it.
Is it only meant for invocation from another app explicitly?
Can't i declare an intent filter in the manifest and then have it respond to broadcast intents?
For example,
I have an intent filter set up like so
<service
android:name=".RemoteShopKeyService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.srini.remoteshopservice.action.FOO" />
</intent-filter>
</service>
And within the Main service class I handle the intent. However how do I invoke the service with an action other than android.intent.action.MAIN via the adb?
I start the service using adb shell am startservice

Related

Unable to start intent service via IMPLICIT intent

AndroidManifest.xml
<application android:name=".MyApplication"
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<service android:name=".MyService"
android:exported="true">
<intent-filter>
<action android:name="android.service.myapp.MyService.actionA"/>
<action android:name="android.service.myapp.MyService.actionB"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
If I use the following code, my service is launched:
Intent intent = new Intent(context, MyService.class);
intent.setAction("android.service.myapp.MyService.actionA");
context.startService(intent);
But my service is not started if I launch it with this code:
Intent intent = new Intent("android.service.myapp.MyService.actionA");
context.startService(intent);
It is insecure to use an "implicit" Intent to start or bind with a Service. Starting with Lollipop, bindService() requires an explicit Intent (your first example where you specify the Context and Class for the Service.) The behavior of startService() is undefined for implicit Intents used to start a service. From the documentation on startService():
The Intent should contain either contain the complete class name of a specific service implementation to start or a specific package name to target. If the Intent is less specified, it log a warning about this and which of the multiple matching services it finds and uses will be undefined.
If you use the explicit form, you can completely remove the <intent-filter> from the manifest: it is not needed. If you need to specify some type of work to be done by the service via the Intent, consider using a extra within the Intent.

Intent filter for SEND action in a broadcast receiver

I want users to be able to Share/Send to my app, but I don't want to start an activity (I just need to send some data on the network, and show a success message popup). I was imagining using a Broadcast Receiver for this, but the intent filter shown below, while it works when in a normal activity, doesn't work for a receiver (i.e. my app doesn't show in the list of things I can share to).
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</receiver>
Should I be using a receiver here, or is there some other way of catching a SEND intent without launching a full activity?
my app doesn't show in the list of things I can share to
Correct. That is because ACTION_SEND is used for activities. The ones who are initiating ACTION_SEND are calling startActivity(). You cannot respond to a startActivity() request with anything other than an Activity.
is there some other way of catching a SEND intent without launching a full activity?
No. You are welcome to use Theme.NoDisplay, Theme.Translucent.NoTitleBar, or something to have an activity with no UI, though. Just don't call setContentView() in onCreate(), but instead do your work (e.g., kick off an IntentService to do your network I/O), and call finish().

how to tell the application it was started from boot up of the device?

Hi I'm writing a application for android that is started from boot up and i wondered if there was a way of telling the application it was started from the boot up of the device? i need it to do something different if the application was manual started (i.e not when the device was started). i am using a BroadcastReceiver to start the application when the device starts.
You could either make two different broadcast receivers one that has ACTION_BOOT_COMPLETED for the intent filter, and another that has the other intent filter that you would use.
Or create one broadcastreceiver that has two intent filters like:
<receiver android:name=".BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="SOMETHING_ELSE"/>
</intent-filter>
</receiver>
and then in the onReceiver do:
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
// do code for phone just powered on
} else {
// do code for phone is already on
}
EDIT:
The above assumes that you use the BroadcastReceiver under two circumstances, which may not be the case judging from your question.
So if you are starting an Activity (or service), then in the BroadcastReceiver code, you could do:
Intent i = new Intent(context, MyClass.class);
i.putExtra("STARTED_FROM_BOOT", true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Then in the activity, you could do:
if (getIntent().hasExtra("STARTED_FROM_BOOT")){
// do your code for when started from boot.
}
Let me know if I need to add anything.
yes, hook a broadcastreceiver with a on boot completed intent in the manifest and when the device boots up that receiver will be fired and you can do whatever you want there
<receiver android:name=".BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

how can a broadcast receiver get the android system to start a livewallpaper service?

LiveWallpapers have the permission BIND_WALLPAPER that allows only the system to bind to it. I want the livewallpaper service to be set as the wallpaper, when the device boots up. For this I have written a broadcast receiver that on boot up is meant to start the wallpaper service, using intents. The component of the livewallpaper is set as the component of the intent. At the moment this can detect the wallpaper service but cannot set it due to the permission BIND_WALLPAPER. The manifest of my application includes as follows
<receiver android:name=".MyStartupIntentReceiver" android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
The broadcast receiver is as follows :
Intent ii = new Intent(Intent.ACTION_MAIN);
ii.addCategory(Intent.CATEGORY_HOME);
ii.setComponent(new ComponentName("com.example.android.livecubes","com.example.android.livecubes.cube1.CubeWallpaper1"));
context.startService(ii);
Ive also tried the above with action.SET_WALLPAPER...category : Launcher/default but everything throws the error permission denied: bind_wallpaper needed. This permission is present at both the service and receiver ends. How can I set the livewallpaper on boot up without chooser/livewallpaper picker ?
This is not possible. You can start the chooser, but you cannot bypass the chooser and actually set the wallpaper in a seamless way. See comment from Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/a22e66002f884718

Broadcast receiver in android

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" />

Categories

Resources