I wanna make a service that when it is active and the user presses the call button, whether in the OS phone app or in the contacts, it prevents outgoing call. I mean instead of running android calling service, i want my service to be run.
Is there any way to do so?
I am really beginner in android programming and i don't really know a lot about android services.
I'll appreciate you to help me.
Thanks a lot.
You have to use BroadcastReceiver for this.
in manifest you have to write
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name=".OutGoingCallListener">
<intent-filter android:priority="0">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
And in class which extends from BroadcastReceiver you have to write
public class OutGoingCallListener extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
//if u want to block particular call then check number or else block all call in thid code
setResultData(null);//Canceling call operation
Toast.makeText(context, "This call is not Allowed", Toast.LENGTH_LONG).show();
} }
Related
Hello I've made an app with a receiver to listen to incoming calls ,
My problem is that when i close (swipe off from list of apps) the receiver doesn't work anymore.
first thing i tried is the receiver itself defined in the android manifest like so:
<receiver
android:name=".demo.CallReceiver"
android:exported="true"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter android:priority="999">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
This works only when the app is open or in the background .
I looked online and saw this - https://stackoverflow.com/a/46889335/7079340
so i made a Service of my own like so (in the manifest):
<service android:name=".Service.CallService" android:enabled="true"
android:exported="false"> <intent-filter>
<action android:name="com.package.name.IRemoteConnection" />
</intent-filter>
</service>
and the class :
public class CallService extends Service {
private static BroadcastReceiver m_Receiver;
#Override
public IBinder onBind(Intent arg0)
{
Log.e("SERVICELOG","bind");
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("SERVICELOG","start command");
return START_STICKY;
}
#Override
public void onCreate()
{
Log.e("SERVICELOG","create");
Receiver();
}
#Override
public void onDestroy()
{
Log.e("SERVICELOG","destroy");
try{
unregisterReceiver(m_Receiver);}catch (Exception e){
Log.e("SERVICELOG"," "+e.getMessage());
}
}
private void Receiver()
{
m_Receiver = new CallReceiver();
}
}
Started it in the oncreate of my Splashscreen and it prints the logs and it works !
Any way to make this work without a service I'm afraid of battery issues and so on ? Thanks !
Android has had many changes from Marshmallow and above about apps that implicitly listener for broadcast in their manifest. The reasoning for this is because several apps would register for broadcast and a new process would be spun up for all the registered app's broadcast receiver to run in (very expensive) thus causing battery drain. What made this worse, was that users have no control over this behavior because the broadcast receiver couldn't be unregistered. To fix this, the engineering team behind Android, only allows for a select few broadcast to be implicitly registered. One is the Device Boot broadcast intent. By stopping apps from implicitly registering Broadcast's, apps have to be manually launched by the user to listen for intents they'd like to be notified of. This prevent several unnecessary apps from waking up to attempt to handle intent.
As for your concern, about "battery issues" I would recommend to you to use the preferred pattern of explicitly registering a BroadcastReciever in your Service and just performance tune to get your code to be as performant as possible. Services are definitely not free, but they aren't automatically heavy objects just by having one started and running; plus they do exist for this exact purpose. Just remember to not do unnecessary work in your service and you should be off to the right start.
When you close your app, it goes directly to onDestroy method. In your service code, you implemented this method. So, in your method you did stop your service programmatically. In short, you must remove it.
I am trying to show a toast message when receiving an incoming call/outgoing call.
The receiver is not working if the app is closed.
I do not want to use Service. Please help me out.
'I am using the below receiver code'
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
if (isConnected(context)) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Call in progress", Toast.LENGTH_LONG).show();
}
}
}
'This is receiver registered in manifest'
<receiver android:name="com.example.android.testapplication.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.new_outgoing_call"></action>
</intent-filter>
</receiver>
Try adding the phone state permission to your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
By default when you register a BroadCastReceiver with AndroidOS that means Receiver always work as the Service part even your application is not working, since you do not have to worry about this problem.
I think the problem is the way you register you Receiver was not correct.
With in/out coming call you should use PhoneStateListener which has overrided method onCallStateChanged. You can use 3 states over there.
Maybe this example will be helpful.
I am working on an application which will start an activity on receiving a ACTION_SCREEN_ON broadcast message.
How to implement this? Can anyone post the code for it?
I know that I have to use BroadcastReceiver. But I need a detailed explanation as I am a beginner.
Create in your application's AndroidManifest.xml a receiver with a intent-filter to catch android.intent.action.SCREEN_ON:
<receiver
android:name="your.app.packagename.Receiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
Then, create the receiver class:
package your.app.packagename;
// imports here
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
// Do whatever you need to be done when screen turns on
}
}
}
Realize that:
You need launch your app at least one time before it starts receiving the ACTION_SCREEN_ON broadcast.
The onReceive() method is expected to run fast. If you are going to do heavy work, launch an AsyncTask or a background thread for.
i am new to android and i want to ask a question about android background services.
i have created a background service and which is activated after every one minute and pop up some message.
i want this service to activate to user whenever user installed another application to phone.
is there any method to check this.
is there any method that check whenever any app is run for the first time and request user for permission?? OR what permission it requires from user??
Waiting for answer.
Thanks in advance for good replies.
Set up a BroadcastReceiver for the android.intent.action.PACKAGE_ADDED intent.
Using Broadcast receiver you can do so as shown below:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//Toast.makeText(arg0, "A New Package has been Installed!!!").show();
}
}
You should register this broadcast receiver in your manifest as shown below:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
</intent-filter>
</receiver>
From the above code a toast will be displayed whenever a new package is displayed. Hope this helps as it had worked for me.
I want to check the internet connection constantly and close the app with a warning message if connection is lost. How can i manage to do that?
Check Internet Connectivity via Phone Background service (such as AlermManager Service) then close the app if no connection found.
thanks.
close the app
Don't try to kill the process and its not recommended way of closing application. Either call finish() on all activities or call moveTaskToBack(true).
For Solution
Here you go.
You will need to register for and handle BroadCastReceiver android.net.conn.CONNECTIVITY_CHANGE
Step 1
Include following permission in manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step2
Let Android know which class will be register for BroadCast Receiver.
<receiver android:name="ConnectivityReceiver_package_name.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Step 3
Put your logic for various Network States.
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);
if(noConnectivity){
//Show Warning Message
//Close Application the way i suggested
}
}
}