How to show notification, even the application does not work

I'm new to android.i create a service to show a notification when onCreat on mainActivity is called

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent a = new Intent(this,myService.class); startService(a); } } 

here is my class of service

 public class myService extends Service{ private NotificationManager mManager; @Override public void onCreate() { mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class); Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis()); intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); mManager.notify(0, notification); } 

but I want, if my application does not work, I can show this service, for example, if my phone time is more than 9, I show this notification only once a day. thank you in advance

+6
source share
2 answers

Notifications are executed in the background, and the application is not required. Although the application does not work, notifications also work, and this is their main task. I offer you a Vogella tutorial and documentation:

http://www.vogella.com/tutorials/AndroidNotifications/article.html

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+5
source
  • You do not want to show notifications while your application is running. It is just annoying and makes (usually) no sense. Of course, there are exceptions.

  • If you want to show a notification at a specific time, you should consider using AlarmManager .

  • If you want to trigger a notification from a server or elsewhere, use Google Cloud Messaging .

+1
source

Source: https://habr.com/ru/post/969779/


All Articles