i am beginner at android. I have two class, and first class is
public class SmsReceiver extends BroadcastReceiver{}
And the second class is
public class SMS extends Activity{}
All I want to do that : when I get an SMS, start activity and do something. But i want to use "service" instead of "activity". I mean when application start, then start service without activity.
is this possible ?
Start your Service from SmsReceiver as:
public class SmsReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
// start service here
Intent intent=new Intent(context,Your_Service.class);
context.startService(intent);
}
else {
}
}
}
and make sure you have registered your service in AndroidManifest.xml as :
<service android:name="com.xxx.xx.Your_Service" />
Yes you can do that by just creating a BroadCastReceiver that calls your Service when your Application Boots. Here is a complete answer given by me. Android - Start service on boot
If you don't want any icon/launcher for you Application you can do that also, just don't create any Activity with
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Just declare your Service as declared normally.
When you got an sms you can start your service by broadcast receiver
#Override
public void onReceive(Context context, Intent intent) {
try {
context.startService(new Intent(context,
YourService.class));
} catch (Exception e) {
Log.d("Unable to start ", "Service on ");
}
and pls make sure you have mention the permission to your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_SMS">
and for sms sending and receiving you can check out this tutorial Sending sms in android
Related
This is what I am using right now:
in the onCreate method:
registerReceiver(bootup, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
the bootup receiver:
BroadcastReceiver bootup = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "received");
}
};
These are both in the MainActivity class of my app. What I want is for the bootup receiver to be called when the bootup of the phone is complete. I need it in the MainActivity class because I need to access a few things from it.
How would I set this? My current solution does not work.
EDIT: The posted solution seems as though it will work for my purpose, and this is what has been suggested by other threads. However, when I put a log statement in the receiver that it ties to, nothing appears in the console. Is this because the application is not running? I have also tried sending a notification with notificationmanager
EDIT 2: I took advice and switched to a broadcastreceiver in a another class for detecting the reboot. It works by simply changing the name attribute on the manifest file. This being the reason why I cant use an inner class receiver doesn't really make any sense compared to what I have seen on other questions. Can someone explain why I can't point the receiver to the inner class one and why I have to use a separate class?
Create a Receiver:
public class BootupReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
And receiver and uses-permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.restarttest.BootupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I'm very new to Android and Programming in general, so I'm playing around with different
tutorials and info gathered here on stackoverflow.
What I would like to accomplish, is having the app with my SMS BroadcastReceiver run as a service, so I can get all the SMS broadcasts when app is in the background.
Also, how can I add a BroadcastReceiver for receiving broadcast of BOOT_COMPLETED and start app automatically?
Would I need several services for this, or is 1 service sufficient? (for detecting SMS + BOOT_COMPLETED continuously)
Currently I have a created a BroadcastReceiver for getting SMS, like this;
public class SMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
.. etc ..
.. etc ..
}
and my AndroidManifest.xml file has receiver and intent-filter with the
additional android.provider.Telephony.SMS_RECEIVED
Getting the SMS broadcast works fine, but I'm not sure where to go from here.
All help is much appreciated :)
Thanks.
To start your service on BOOT_COMPLETED event and to receive SMS intent continuously.
AndroidManifest.xml:
<receiver android:name="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SMSService.class);
context.startService(service);
}
}
SMSService.java:
public class SMSService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (Intent.BOOT_COMPLETED.equals(action)) {
//write your code to process BOOT_COMPLETED intent here
}
else if(Intent.SMS_RECEIVED.equals(action)) {
//Write your code for processing SMS intent here
}
}
}
As, Fildor has pointed out, it is unnecessary to start service on BOOT_COMPLETED intent. InentService would do the work. So, above two code snippets are not required. Just the last snippet would do the work.
My question is very simple and I know the function can be preformed. I want to start activity via text message like "WHERE IS MY DROID" how can this be done? Please provide any information. I think SMSReceiver BroadcastReceiver is in the correct direction but I am not sure.
You need to register an Broadcast Receiver in AndroidManifest.xml for Receiving SMS_RECIVERD Broadcast as:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Add permission in AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
in your Broadcast Receiver code start your Application as:
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//context.startService(new Intent(context, YourService.class));
//Start activity as:
Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
context.startActivity(intent24);
}
}
}
NOTE : For starting your Activity from background you need to set Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_FROM_BACKGROUND flags in intent for starting activity from background.
Check out this guy's sample code for intercepting SMS's http://imran-android-sms.blogspot.com/2011/03/receive-sms-on-android.html#more
From there you just want to fire off an intent to whatever your new activity should be.
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.
I'm monitoring incoming SMSs.
My app is working perfectly with a BroadcastReceiver. However it is working from an Activity and would like to keep the BroadcastReceiver running all the time (and not just when my Activity is running).
How can I achieve this? I've looked through the lifecycle of the BroadcastReceiver but all that is mentioned in the documentation is that the lifecycle is limited to the onReceive method, not the lifecycle of keeping the BroadcastReceiver checking for incoming SMS.
How can I make this persistent?
Thanks
You need to define a receiver in manifest with action name android.intent.action.BOOT_COMPLETED.
<!-- Start the Service if applicable on boot -->
<receiver android:name="com.prac.test.ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Make sure also to include the completed boot permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Use Service for this to make anything persist. And use receivers to receive Boot Up events to restart the service again if system boots..
Code for Starting Service on boot up. Make Service do your work of checking sms or whatever you want. You need to do your work in MyPersistingService define it your self.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ServiceStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.prac.test.MyPersistingService");
i.setClass(context, MyPersistingService.class);
context.startService(i);
}
}
Service or Boot Completed is not mandatory
In fact, you don't need to implement a Service or register to android.intent.action.BOOT_COMPLETED
Some examples shows how to register/unregister a BroadcastReceiver when activity is created and destroyed. However, this is useful for intents that you expect only when app is opened (for internal communication between Service/Activity for example).
However, in case of a SMS, you want to listen to the intent all the time (and not only when you app is opened).
There's another way
You can create a class which extends BroadcastReceiver and register to desired intents via AndroidManifest.xml. This way, the BroadcastReceiver will be indepedent from your Activity (and will not depend from Activity's Life Cycle)
This way, your BroadcastReceiver will be notified automatically by Android as soon as an SMS arrive even if your app is closed.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest>
...
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application>
....
<receiver android:name=".MyCustomBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
MyCustomBroadcastReceiver.java
public class MyCustomBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent != null) {
String action = intent.getAction();
if(action != null) {
if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {
// DO YOUR STUFF
} else if (action.equals("ANOTHER ACTION")) {
// DO ANOTHER STUFF
}
}
}
}
}
Notes
You can add others intent-filters to AndroidManifest and handle all of them in same BroadcastReceiver.
Start a Service only if you will perform a long task. You just need to display a notification or update some database, just use the code above.
Add Broadcast Reciever in manifest:
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Create Class BootReciever.java
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
// +++ Do Operation Here +++
}
}
}
Beside #Javanator answer I would like to include a case for Android version of (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) In my case this is working for Android SDK 29 (10)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context,FloatingWindow.class));
} else {
context.startService(new Intent(context, FloatingWindow.class));
}
use this code and also mention the broadcast in Manifest also:
public class BootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
//write code here
}
}
}
I just want to mention that in case of some Chinese phone brands (e.g. MI), you need to go to Settings and give autostart permission to your app.
Otherwise the battery optimisation feature will kill your service in background and broadcast receiver will not work.
So you can redirect your user to Settings and ask them to give that permission.