BroadcastReceiver in Service Android - android

I want to add a BroadCastReciver in a service. The BroadCastReceiver listens to phone calling. My problem is that the broadcast doesn't work in service (when I run the app it worked but when I go out of my app it didn't work).
MainActiviy.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this,FirstService.class));
}
}
PhoneStateBrodcastRecevier.java:
public class PhoneStateBrodcastRecevier extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener(context);
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e){}
}
}
FirstService.java:
public class FirstService extends Service {
BroadcastReceiver myreciReceiver;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
myreciReceiver=new PhoneStateBrodcastRecevier();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("android.intent.action.PHONE_STATE");
registerReceiver(myreciReceiver, intentFilter);
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("ddddddd", "start");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("ddddddd", "finish");
}
}
And my manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneStateBrodcastRecevier">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<service android:name=".FirstService"/>
</application>

When your target is to listen for phone state changes in the Service class itself. You can try to define it internally within the Service. This will decrease resource usage too as the system is not burdened with handling the lifecycle of another component. In your Service:
private final PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// Call receive state
if (state != TelephonyManager.CALL_STATE_IDLE) {
// Do something
}
}
};
Then you can register it to listen as (probably in onStartCommand()):
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
And then unregister it from listening in onDestroy() (or whenever you are done):
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);

Do something when the phone rings
Solution
This can be achieved by implementing a Broadcast Receiver and listening for a TelephonyManager.ACTION_PHONE_STATE_CHANGED action.
Discussion
If you want to do something when the phone rings you have to implement a broadcast receiver which listens for the TelephonyManager.ACTION_PHONE_STATE_CHANGED intent action. This is a broadcast intent action indicating that the call state (cellular) on the device has changed.
Create a class IncomingCallInterceptor which extends BroadcastReceiver.
Override the onReceive method to handle incoming broadcast messages.
The EXTRA_STATE intent extra in this case indicates the new call state.
If (and only if) the new state is RINGING, a second intent extra EXTRA_INCOMING_NUMBER provides the incoming phone number as a String.
We extract the number information from the EXTRA_INCOMING_NUMBER intent extra.
Note: Additionally you can act on a state change to OFFHOOK or IDLE when the user picks up the phone or ends/rejects the phone call respectively.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class IncomingCallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String msg = "Phone state changed to " + state;
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
msg += ". Incoming number is " + incomingNumber;
// TODO This would be a good place to "Do something when the phone rings" <img src="http://androidlabs.org/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley">
}
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
We have to register our IncomingCallInterceptor as a within the element in the AndroidManifest.xml file.
We register an 'intent-filter'…
and an action value which registers our receiver to listen for TelephonyManager.ACTION_PHONE_STATE_CHANGED broadcast messages.
Finally we have to register a so we are allowed to listen to phone state changes.
<receiver android:name="IncomingCallInterceptor">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>

Related

I cannot keep the broadcast receiver running after the application is closed

Edit
Everybody thank you for reply,
unfortunately it was impossible to do this work at after Android Pie
https://nllapps.com/apps/acr/android9.htm
https://issuetracker.google.com/issues/112602629
I cannot keep the broadcast receiver running after the application is closed, I tried many methods but failed.
public class BoothService extends Service {
private final Handler mHandler = new Handler();
public BoothService() {
}
private String INCOMING_CALL_ACTION = "android.intent.action.PHONE_STATE";
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String mAction = intent.getAction();
if(mAction.equals(INCOMING_CALL_ACTION)) {
Toast.makeText(context, "BoothService", Toast.LENGTH_SHORT).show();
}
}
};
public void onCreate() {
IntentFilter intentToReceiveFilter = new IntentFilter();
intentToReceiveFilter.addAction(INCOMING_CALL_ACTION);
this.registerReceiver(receiver, intentToReceiveFilter, null, mHandler);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
This service run from activitymain
Intent BS = new Intent(ActivityMain.this, BoothService.class);
startService(BS);
my purpose is to capture incoming calls, even if the application is closed
My manifest
<receiver
android:name=".RingReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
permission,
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
by the way this code works in the emulator ,but it doesn't work on real devices I've tried
Try to use JobService for register your implicit broadcast because after nought implicit broadcast is not working when app is close.
So register your broadcast reciever to jobservice and use schedule that service with jobservice.
Or you can refer this link as well
https://www.journaldev.com/23653/android-oreo-implicit-and-explicit-broadcast-receiver

Starting Android application at boot completion: is my solution overly complicated?

I have an application that I would like to have automatically start following boot completion. The following code seems overly complicated and I get erratic application starts when swiping to a neighbouring workspace.
What am I missing here? I have an activity class, a service class, as well as a broadcast receiver. Below is my code (in that order) followed by the manifest.
public class BlueDoor extends Activity implements OnClickListener{
Button btnExit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
btnExit = (Button) this.findViewById(R.id.ExitButton);
btnExit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ExitButton:
System.exit(0);
break;
}
}
}
service.class
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(this, BlueDoor.class);
startActivity(callIntent);
// do something when the service is created
}
}
broadcast receiver
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluedoor"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".StartBlueDoorAtBootReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".BlueDoorStartService" >
</service>
<activity
android:name=".BlueDoor"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
UPDATE Solution(s), 10/22/2015:
Changing the service to:
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
and the receiver to:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Start Service On Boot Start Up
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
//Start App On Boot Start Up
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
resulted in a working configuration using a service w/no misbehaving. However deleting the service all together and modifying the receiver thus:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
also resulted in a functional as well as a more concise configuration that starts the application following boot completion.
Your BroadcastReceiver calls
context.startService(serviceIntent)
so the service will be created if it doesn't exist yet (which will be the case shortly after booting) and thus start the activity from its onCreate() method. So the app works, to a certain extent.
BUT when you call startService(), the system always calls the service's onStartCommand() method. You did not override that method, so the system uses the standard implementation from class android.app.Service.
As you can read on grepcode.com, the method will return a value like START_STICKY by default. This tells the system to keep the service alive until it is explicitly stopped.
In your case, I suppose the system reacted to the swiping by temporarily killing and then reanimating (= creating) the service, which in turn started your activity.
Some information on the service lifecycle can be found here.
What you can do:
Override onStartCommand() to start the activity from there instead of from onCreate(). Then use stopSelf(int) like described here
One last thing: when exiting from the activity, don't use System.exit(0) but call finish() instead, see this SO answer for "why".

listen to intent in background

I am new to android, I have created intent's like this -
<receiver android:name=".IncommigCallListener" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".OutgoingCallReciever" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Now i created a service like this -
<service
android:name=".CallLogger"
android:exported="false"/>
Class CallLogger
public class CallLogger extends IntentService {
public CallLogger(String name) {
super(name);
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("service started");
}
}
I don't want to have any activity in my application, i just want to start the service so that it can work in background and receive PHONE_STATE and NEW_OUTGOING_CALL intent.
When i start this application, it doesn't log anything on PHONE_STATE or NEW_OUTGOING_CALL intent.
How can start service in background without using any activity ?
Edit :
public class OutgoingCallReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
and
public class IncommigCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String incommingCallNumber = incomingNumber;
System.out.println("incomming call : " + incomingNumber);
break;
}
}
}
Just start service in your BroadcastReceiver's onReceive method. As you are registering BroadcastReceiver in AndroidManifist, It will always listen for Broadcasts even if application is not running (OS will run it for you).
Example
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
EDIT
To start a service on Boot completed you can do something like this.
1) Add permission to your Manifist :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) Register your Broadcast Receiver with BOOT COMPLETE action.
<receiver android:name="com.example.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3) In BootBroadcastReceiver.java:
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent );
}
}
You should be able to do something like this in your receiver.
public class OutgoingCallReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent service = new Intent(context, CallLogger.class);
context.startService(service);
}
}
You need to create an intent and call startService() on it to "launch" the service.
Also for what it's worth you should get out of the habbit of System.out.println use Log.d(tag,msg) to print debugging information to the logcat. You can switch the d to other letters if you want to print in different channels.
Why nothing gets printed is only due to the problem that System.out.println does not work in Android! Where do you think the background process will "print" this thing?
You need to change that to Log.d(tag, msg) and then check your logcat to see the output! Otherwise I guess your code might be running properly.

Bring your app to the front when S Pen detached in android note?

I want to open my application immediately when S Pen is detached , How can you do this, if the methods put under onSPenDetached is only called when my application is opened again?
Thanks,
Chandu
The following works on my Galaxy Tab A 9.7 with S-Pen (SM-P550) running Android 5.0.2.
Attaching and detaching the stylus creates Broadcast Intents of type com.samsung.pen.INSERT with a booleanExtra named penInsert of false if detached and true if put back into the device.
Thus a Broadcast Receiver can be created that filters this kind of events. The following code is for such a Broadcast Receiver which starts OneNote if the stylus is detached:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SPenDetachIntentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent penInsertIntent) {
if (!penInsertIntent.getBooleanExtra("penInsert", true)) {
try {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.onenote");
context.startActivity(launchIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In the Manifest file you need to declare it as a receiver listening for com.samsung.pen.INSERT Broadcast Intents with an intent filter. The following entry in a project's AndroidManifest.xml declares SPenDetachBroadcastReceiver, generates an instance and makes it listening for com.samsung.pen.Insert Broadcast Intents:
<receiver
android:name=".SPenDetachIntentBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.samsung.pen.INSERT" />
</intent-filter>
</receiver>
The advantage over using registerSPenDetachmentListener on an SPenEventLibrary object to register a Service with an onSPenDetached method implemented is that you do not need any additional library files and you also do not need additional permissions.
You will need to create a BroadcastReceiver and a Service.
The service:
public class SPenService extends Service {
SPenEventLibrary mSPenEventLibrary = new SPenEventLibrary();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mSPenEventLibrary.registerSPenDetachmentListener(this, new SPenDetachmentListener() {
#Override
public void onSPenDetached(boolean bDetached) {
if (bDetached) {
Toast.makeText(SPenService.this, "S Pen Detached", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SPenService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(SPenService.this, "S Pen Inserted", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
mSPenEventLibrary.unregisterSPenDetachmentListener(this);
}
}
The receiver:
public class SPenReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
context.startService(new Intent(context, SPenService.class));
}
}
}
The manifest (inside the <application> tag):
<receiver android:name=".SPenReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".SPenService" >
</service>

ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working?

I'm trying to turn WiFi off when the screen is OFF (locked), and turn it on again when screen is ON (unlocked).
I made a BroadcastReceiver; put in manifest this code:
<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
and this is the class MyIntentReceiver:
package org.androidpeople.boot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyIntentReceiver extends BroadcastReceiver {
// Called when boot completes
public static boolean startup;
#Override
public void onReceive(Context context, Intent intent) {
// Set what activity should launch after boot completes
System.out.println("Intent Action: " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
System.out.println("locked : ACTION_SCREEN_OFF");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("not locked : ACTION_SCREEN_ON ");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("User Unlocking it ");
}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// this to indicate that program is running
// automaticlly not manually by user
startup = true;
System.out.println("Automatic BOOT at StartUp");
Intent startupBootIntent = new Intent(context, LaunchActivity.class);
startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupBootIntent);
}
}
}
And the result is - both ACTION_SCREEN_ON
and ACTION_SCREEN_OFF never fired!
USER_PRESENT and BOOT_COMPLETED worked fine but the other did not. I'm using an emulator, not a real device - can this cause the problem?
Any help?
I need to catch the screen on and off in order to enable/disable WiFi
to save battery.
Thanks in advance
To capture the SCREEN_OFF and SCREEN_ON actions (and maybe others) you have to configure the BroadcastReceiver by code, not through the manifest.
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);
It's tested and works right.
You cannot catch those intents through XML (I forget why). However, you could use a Service that registers a BroadcastReceiver member in its onStartCommand() and unregisters it in its onDestroy(). This would require the service to be running in the background, constantly or as long as you need it to, so be sure to explore alternative routes.
You could define the BroadcastReceiver in your Service class like so:
private final class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//stuff
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//other stuff
}
}
}
For a slightly complicated example, but one that shows how the BroadcastReceiver and Service interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.

Categories

Resources