Android 即使应用程序未运行,如何显示通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23836699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:30:38  来源:igfitidea点击:

How to show a notification even app is not running

android

提问by

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 ? thanks in advance

但我想如果我的应用程序没有运行,我可以显示此服务,例如,如果我的电话时间大于 9,我一天只显示一次此通知。有什么想法吗?提前致谢

回答by CanDroid

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

回答by SimonSays

  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.

  3. If you want to trigger the notification from a server or wherever else, use Google Cloud Messaging.

  1. 您不想在应用运行时显示通知。这很烦人,而且(通常)没有意义。当然也有例外。

  2. 如果要在特定时间显示通知,则应考虑使用AlarmManager

  3. 如果您想从服务器或其他任何地方触发通知,请使用Google Cloud Messaging