作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 android 的初学者。我创建了一个服务来在调用 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);
}
}
这是我的服务等级
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);
}
但我想如果我的应用程序没有运行,我可以显示此服务,例如,如果我的电话时间大于 9,我一天只显示一次此通知。有什么想法吗?提前致谢
最佳答案
通知在后台运行,不需要运行应用程序。当应用程序未运行时,通知也起作用,这是它们的主要任务。我向您推荐 Vogella 的教程和文档:
http://www.vogella.com/tutorials/AndroidNotifications/article.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
关于android - 如何在应用程序未运行时显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836699/
我是一名优秀的程序员,十分优秀!