[英]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
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. 当然也有例外。
If you want to show a notification at a specific time, you should consider to use the AlarmManager . 如果要在特定时间显示通知,则应考虑使用AlarmManager 。
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.