Android Background service actived on any app installed - android

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.

Related

How to trigger BIND_NOTIFICATION_LISTENER_SERVICE after it stops automatically

I'm doing a React Native app (But my problem is mostly related to Android) where I'll capture notifications in the Native Android code and then give the captured data to Javascript via the callback.
I am using BIND_NOTIFICATION_LISTENER_SERVICE service and specified a class NotificationListenerService for the same in the Manifest file.
AndroidManifest.xml
<service
android:name=".NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:enabled="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
NotificationListenerService.java
public class NotificationListener extends NotificationListenerService {
Context context;
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
// this method is not getting executed after 2 or 3 days of installation
Log.d("KBTCHECK", "NotificationListener - onCreate");
context = getApplicationContext();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
// this method is not getting executed after 2 or 3 days of installation
Log.d("KBTCHECK", "NotificationListener - onNotificationPosted");
// My Logic here..
Intent msgrcv = new Intent("NOTIFICATION_POSTED_KBT");
msgrcv.putExtras(extras);
// Broadcasting it, so I'll capture in another module and send it to Javascript
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
}
Here, I'm not triggering the NotificationListenerService manually. I hope specifying it in Manifest makes the Android system to trigger it automatically.
Problem:
NotificationListenerService's onCreate or onNotificationPosted not being called on receiving notification after 2/3 days of installation while receiving notification.
(I need to disable/enable the notification access manually to get it to work again)
My Expectation:
Trigger the service in specific time interval so that even if it's stopped by android system, it should start again automatically.
If I trigger manually, I can schedule it to trigger in any time interval but since it's being triggered automatically by Android, I don't find a way (Or I don't know how to) to execute the trigger action in the specific time interval.
Please guide me with the correct path to achieve the expected behavior.
Thanks, your time and help will be appreciated.... (:

Listen for user changed event (multi user device)

I've an app that starts itself if the phone is booted. A user told me his phone is used by two people, one of them is using my app and one not.
So I need some event to listen to when the user is switched, so that I can start my apps service if the correct user is using the phone. Anything I can use for that?
Edit
I'm listening to the boot event with a broadcast receiver registered in the manifest, so I know what this is. But I could not find anything suitable for switching users on a device
You need to look for something called BroadcastReciever in android. They are used to capture events such as camera click, phone booting up, screen unlocked etc... These events have a callback called onReceive where you can implement your login.
It's quite easy and you can Google it.
In your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your application element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}

I want to use broadcast receiver even if the app is closed

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.

How to prevent outgoing call in android with a service?

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();
} }

Start Service on boot but not entire Android app

I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.

Categories

Resources