I am not able to show notification from background. In mainactivit i did noy call startService method. So what should i need to implement to show notification from below code. please anyone help me.Please help me.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
BeaconService.Java
public class BeaconService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
private void showNotification() {
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_loc)
.setContentTitle("Welcome to Brillio")
.setContentText("Hello Mansur, Welcome to Brillio.")
.setPriority(2)
.setOnlyAlertOnce(false);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
mBuilder.setOngoing(true);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2001, mBuilder.build());
}
}
BeaconReceiver.java
public class BeaconReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, BeaconService.class);
context.startService(startServiceIntent);
}
}
menifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".BeaconService"
android:enabled="true"
android:exported="true" />
<receiver android:name=".BeaconReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Error show in Console
DEVICE SHELL COMMAND: am start -D -n "com.example.serviceexamp/com.example.serviceexamp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet.
open: Permission denied
open: Permission denied
Related
I wanted to set alarms that played audio files. Yet they don't, according to the log, the alarms are not executed. This is my code:
<manifest ...>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
...>
<service android:name=".S1" />
<activity
...>
...
</activity>
<receiver
android:name=".A1" >
</receiver>
<receiver
android:name=".A2">
</receiver>
</application>
</manifest>
and
public class S1 extends Service {
public void al(Class Klasse, long Wann)
{
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this.getApplicationContext(), Klasse);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, Wann, pendingIntent);
Log.i("Debug","Alarm");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
al(A1.class,time.getTimeInMillis());
al(A2.class,time.getTimeInMillis()+(5*1000));
//...
return START_STICKY;
}
}
and
class A1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...
Log.i("Debug","Play Audio A1");
}
}
class A2 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...
Log.i("Debug","Play Audio A2");
}
}
In the logs, I see "Alarm" but neither "Play Audio A1" nor "Play Audio A2".
I am using a Huawei P20 and I followed https://dontkillmyapp.com/huawei. But they still don't fire.
i want to start my service even if user force stop the app or some other app force stop my app.
Here is my code.
#Override
public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
AlarmManager alarmMgr = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
Intent i = new Intent(this, LocationUpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, i, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10, pendingIntent);
}
I start service in onDestroy().This code will work for me if any one have more efficient way to do this please comment on it.
#Override
public void onDestroy() {
super.onDestroy();
startLocationUpdateService();
}
First of all, it is really very bad pattern to run service forcefully against
the user's willingness.
Anyways, you can restart it by using a BroadcastReceiver which handles the
broadcast sent from onDestroy() of your service.
StickyService.java
public class StickyService extends Service
{
private static final String TAG = "StickyService";
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("YouWillNeverKillMe"));
}
}
RestartServiceReceiver.java
public class RestartServiceReceiver extends BroadcastReceiver
{
private static final String TAG = "RestartServiceReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "onReceive");
context.startService(new Intent(context.getApplicationContext(),
StickyService.class));
}
}
Declare the components in manifest file
<service android:name=".StickyService" >
</service>
<receiver android:name=".RestartServiceReceiver" >
<intent-filter>
<action android:name="YouWillNeverKillMe" >
</action>
</intent-filter>
</receiver>
Start the StickyService in a Component (i.e. Application, Activity, Fragment):
startService(new Intent(this, StickyService.class));
how to get notification before open app or when i kill application still notification show on mobile device. or when i start phone i want notification but application should not running.
public class BeaconService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
private void showNotification() {
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_loc)
.setContentTitle("Welcome to Brillio")
.setContentText("Hello Mansur, Welcome to Brillio.")
.setPriority(2)
.setOnlyAlertOnce(false);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2001, mBuilder.build());
}
}
BeaconReceiver.java
public class BeaconReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intentLunch = new Intent(context, BeaconService.class);
context.startService(intentLunch);
}
}
menifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".BeaconService"/>
<receiver android:name=".BeaconReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
There is no straightforward way to start a service on just installing the app, without user opening it first time.
you just check these two links for more
1) How to start a Service when .apk is Installed for the first time
2)How to start android service on installation
To make presistant Notification, add this line:
builder.setOngoing(true)
Hope this will helps you
I'm building an enterprise application that needs to get some information about the employees' rooted phones to do corporate management.
This thread needs to run each five minutes.
I'm using an Activity that is started by a broadcast(BOOT_COMPLETED) when the android boots up, and it starts an infinite thread to send this information to server.
My current problem is my application is being killed by android after the user opens a lot of others apps.
What would be the better way to keep a thread running in background to send this information to server?
Main Application Class
public static void startService(Context mContext){
try{
//Schedule Service.
scheduleService(mContext);
//Call onUpdate.
onUpdate();
}catch (Exception o){
Utilities.log(o.toString());
}
}
public static void scheduleService(Context mContext){
try{
final int NOTIFICATION_INTERVAL = 5 * 60 * 1000;
Intent mIntent = new Intent(mContext, ServiceReceiver.class);
AlarmManager mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 0);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), NOTIFICATION_INTERVAL, mPendingIntent);
}catch (Exception o){
Utilities.log(o.toString());
}
}
ServiceReceiver
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context mContext, Intent intent) {
Utilities.log("Service Received");
//Start Service.
MyApplication.startService(mContext);
}
}
AndroidManifest
<receiver
android:name=".BootUpReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".ServiceReceiver"/>
BootUpReceiver
public class BootUpReceiver extends BroadcastReceiver
{
public void onReceive(Context mContext, Intent mIntent){
Utilities.log("BootUp Received.");
//Start Service.
MyApplication.startService(mContext);
}
}
create a static broadcast receiver for Repeating Alarms and start Intent Service from broadcast don't use infinite Thread
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationStatus.setupNotification(context); // if you restart your phone
}
}
class NotificationStatus{
//Call only one time from app from any activity
public static void setupNotification(Context context) {
final int NOTIFICATION_INTERVAL = 5 * 60 * 1000;
Intent myIntent1 = new Intent(context, NotificationReceiver.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1, myIntent1, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), NOTIFICATION_INTERVAL, pendingIntent1);
}
}
public class NotificationReceiver extends BroadcastReceiver {
private static final int mNotificationId = 0;
#Override
public void onReceive(Context context, Intent intent) {
//start your services here for sending data
Intent intent1 = new Intent(context, SyncService.class);
context.startService(intent1);
}
}
public class SyncService extends IntentService {
public SyncService(String name) {
super(name);
}
#Override
protected void onHandleIntent(Intent intent) {
//Write code here for sending data to server
}
}
AndroidManifest
<receiver android:name="NotificationReceiver" />
<receiver android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
Define Service in Manifest
<service android:name=".SyncService"/>
You need to make your application an Android service.
I read several threads about repeat asynchronous tasks in background, I first used this way: https://stackoverflow.com/a/6532298 but for some reasons, it seems that after sometime (several hours), it stopped.
So, now I am using this way, but I don't know if this is a good way to proceed:
BroadcastReceiver
public class RetrieveDataTaskBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences mSharedPreferences = context.getSharedPreferences(MY_PREF, 0);
int delayInMs = mSharedPreferences.getInt("set_delay_refresh", 20)*60*1000;
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, RetrieveDataService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.cancel(pi);
if (delayInMs > 0) {
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delayInMs,
delayInMs, pi);
}
}
}
My service class:
public class RetrieveDataService extends Service implements OnRefreshInterface {
private Context context;
private PowerManager.WakeLock mWakeLock;
private static final String TAG = "REFRESH_SERVICE";
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void handleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire();
//do the work
callAsynchronousTask();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
public void onDestroy() {
super.onDestroy();
mWakeLock.release();
}
#Override
public void onCreate() {
super.onCreate();
context = this;
callAsynchronousTask();
}
public void callAsynchronousTask() {
//My asynchronous task => execute a class that extends AsyncTask
[...]
}
#Override
public void onRefreshInterface(int cb_val1, int cb_val2) {
//Callback when refresh is done
[...]
}
}
androidmanifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<service
android:name="com.example.package.RetrieveDataService"
android:enabled="true"
android:label="Refresh Data">
</service>
<receiver
android:name="com.example.package.RetrieveDataTaskBroadcast"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
I also added a way to launch the service when the app starts:
MainActivity:
#Override
protected void onResume() {
super.onResume();
Thread t = new Thread(){
public void run(){
SharedPreferences mSharedPreferences = getSharedPreferences(PREF, 0);
int delayInMs = mSharedPreferences.getInt("set_delay_refresh", 20)*60*1000;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(MainBaseActivity.this, RetrieveDataService.class);
PendingIntent pi = PendingIntent.getService(MainBaseActivity.this, 0, i, 0);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delayInMs,
delayInMs, pi);
}
};
t.start();
}
Thank you for your help and advice.
You dont need a service to launch the repeated task when the device starts. Your task will never run when the device is off.
You can set a repeating alarm using Alarm Manager.
If the trigger time you specify is in the past when the device was off, the alarm triggers immediately when the device turns on.
Check this - https://developer.android.com/training/scheduling/alarms.html