在android中创建自定义通知

发布于 2024-11-16 04:40:23 字数 202 浏览 2 评论 0原文

在我的 android 项目中,我需要在发生特定事件时显示通知。 在我的应用程序中,我有一项在启动完成时启动的服务。该服务启动后台线程。这些线程引发事件。 现在的需求是,当事件发生时,我想在屏幕中间显示通知。这个通知类似于带有单个按钮的对话框。通知屏幕一直存在,直到用户单击按钮,这意味着当用户单击按钮时,屏幕将消失。我不知道如何在屏幕中间创建通知。

任何帮助将不胜感激..

In my android project I need to show the notification when the certain event has occurred.
In my application, I have a service which starts at boot completion. And this Service starts the background threads. These threads raises the event.
Now the requirement is, when the event occurred , i want to show the notification in the middle of the screen.this notification is similar to the dialog box with single button. The notification screen exist until the user clicked on the button,means when the user clicked on the button,the screen will disappear.I don't know how the create the notification in the middle of the screen.

Any help will be appreciated..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-11-23 04:40:23

在方法中设置此代码并通过传递要显示的数据来调用此方法。

这是 Android 弹出窗口。它将显示在屏幕中间。

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
        alt_bld.setMessage("set target message data").setCancelable(false).setPositiveButton("yes", new OnClickListener() { 

 public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} }).setNegativeButton("No", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub dialog.cancel(); 
} });

 AlertDialog alert = alt_bld.create(); alert.setTitle("Popup button"); alert.show();

有关更多信息,请参阅:http://developer.android.com/reference/ android/app/Dialog.html ,

set this code in a method and call this method with passing data which you want to display.

this is android popup. it will display middle of the screen.

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
        alt_bld.setMessage("set target message data").setCancelable(false).setPositiveButton("yes", new OnClickListener() { 

 public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} }).setNegativeButton("No", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub dialog.cancel(); 
} });

 AlertDialog alert = alt_bld.create(); alert.setTitle("Popup button"); alert.show();

For more info, refer this: http://developer.android.com/reference/android/app/Dialog.html ,

如此安好 2024-11-23 04:40:23

Service 无法创建 UI,因此您需要创建一个包含所需 UI 的 Activity,并将 Intent 从 Service 发送到 Activity。

更新:

我强烈建议您从基础知识 要了解这里发生的一切,但基本上您需要:

  1. 创建一个看起来像您想要的 UI 的活动
  2. 发送一个意图以从您的服务启动此活动

对于#1,您有两个我能想到的明显选项,其中任何一个都应该有效:

对于#2,这是基本的 Android:

startActivity(new Intent(this, ActivityClass.class));

A Service can't create UI, so you'll need to create an Activity that contains the UI you want, and send an Intent from the Service to the Activity.

Update:

I really recommend that you start from the fundamentals to understand everything that's going on here, but basically you need to:

  1. Create an activity that looks like the UI you want
  2. Send an intent to start this activity from your service

For #1, you have two obvious options that I can think of, either of which should work:

For #2, this is basic Android:

startActivity(new Intent(this, ActivityClass.class));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文