简体   繁体   English

如何显示通知甚至应用程序未运行

[英]How to show a notification even app is not running

i'm beginner in android.i create a service to show a notification when onCreat on mainActivity called 我是android.i的初学者创建一个服务,在mainActivity上调用onCreat时显示通知

    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 service class 这是我的服务类

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 app is not running i can show this service for example if my phone time is bigger than 9 i show this notification only one time in a day.any ideas ? 但我想如果我的应用程序没有运行我可以显示此服务,例如,如果我的手机时间大于9我只在一天中显示此通知一次。任何想法? thanks in advance 提前致谢

Notifications are running on background and application running is not needed. 通知在后台运行,不需要运行应用程序。 While application is not running, notifications also work and this is the main mission of them. 虽然应用程序没有运行,但通知也有效,这是他们的主要任务。 I suggest you Vogella's tutorial and documentation: 我建议您使用Vogella的教程和文档:

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

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

  1. You don't want to show notifications while your app is running. 您不希望在应用运行时显示通知。 That is just annoying and has (usually) no point. 这只是烦人的,并且(通常)没有意义。 There are of course exceptions. 当然也有例外。

  2. If you want to show a notification at a specific time, you should consider to use the AlarmManager . 如果要在特定时间显示通知,则应考虑使用AlarmManager

  3. If you want to trigger the notification from a server or wherever else, use Google Cloud Messaging . 如果要从服务器或其他任何位置触发通知,请使用Google Cloud Messaging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想显示通知,即使我的Android应用正在运行 - I want to show notification even if my Android app is running 如何在应用未运行时显示通知 - how to show notification when app is not running 即使应用未运行,如何连续更新通知? - How to continuously update notification even when app is not running? 即使应用未在Android中运行,如何按时间或间隔发送通知? - How to send notification by timing or interval even the app is not running in Android? 即使应用未运行,如何显示通知 - How do I show notifications even if the app is not running 即使应用程序未运行,如何通过通知点击触发应用程序中的弹出窗口 - How to trigger pop-up in app via notification click even when app is not running 如果应用正在运行,则不会在状态栏中显示通知 - Not show notification in status bar if app is running 如何安排3个事件通过android中的(通知栏)通知用户,即使我的应用未运行,该事件也会运行 - How to schedule 3 events to notify user via (notification bar) in android which runs even when my app is not running 即使应用未运行,每分钟也会在特定时间显示Phonegap通知 - Phonegap display notification on specific time every minute even the app is not running 确保即使未打开应用程序也能在 Android 上显示通知 - Ensure that notification show up on Android even if app is not opened
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM