gpt4 book ai didi

android - 如何在应用程序未运行时显示通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:51 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com