如何在Android的通知栏中显示简单通知?请帮助我使用最简单的解决方案。如何在android中显示简单通知?
回答
我假设你是问有关通知,notificationbar.If其如此,试试这个代码,
private void showNotification(String eventtext, Context ctx) {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.noti_icon,
text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(ctx, "Title", eventtext,
contentIntent);
// Send the notification.
mNM.notify("Title", 0, notification);
}
是mNM吗?我的通知? –
@WHK:它的通知管理器 –
这很容易。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A9BCF5"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="Create Notification" >
<Button>
<LinearLayout>
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//We get a reference to the NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.notification_icon, MyText, System.currentTimeMillis());
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears
String MyNotificationTitle = "Medicine!";
String MyNotificationText = "Don't forget to take your medicine!";
Intent MyIntent = new Intent(Intent.ACTION_VIEW);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID , mNotification);
//We are passing the notification to the NotificationManager with a unique id.
}
});
}
}
您可以将声音或振动添加到通知,以及:在这个例子中单击该按钮时,通知被触发。欲了解更多信息,你可以看到this教程。
尽管上面提到的通知构造函数已被弃用......仍然是+1 ... Notification.Builder()是创建通知的新方法。 –
谢谢@JohnnyWu – erdomester
- 1. 如何在Android中显示此通知?
- 2. 如何在android中显示通知?
- 3. 如何在提交简单表单后显示通知
- 4. Android简单通知
- 5. Android简单通知
- 6. 在android中显示通知
- 7. 如何显示抬头通知android
- 8. Android单挑通知没有显示
- 9. 如何在单一通知中显示单个按钮
- 10. 如何在IntelliJ中显示通知?
- 11. 如何在IOS中显示通知
- 12. 如何在iWatch中显示通知?
- 13. 如何在asp.net中显示通知
- 14. 在通知中显示新表单
- 15. 在Android中,如何获取给定通知ID的当前显示通知?
- 16. Android通知不会显示
- 17. Android通知不显示
- 18. Android - notificationManager.notify不显示通知
- 19. 通知不显示的Android
- 20. Android通知未显示
- 21. 通知未显示android
- 22. Android通知从未显示
- 23. android通知remoteviews不显示
- 24. Android O通知未显示
- 25. Android穿不显示通知
- 26. 在android中显示通知清除通知
- 27. GCM通知不会显示在通知栏中。 Android
- 28. 如何在android中显示特定数量的通知
- 29. 如何在Android中显示常量通知?
- 30. 如何在Honeycomb +设备的Android通知中显示时间戳?
你谷歌“机器人通知”?第一个结果似乎是adroid开发者“如何创建通知”的指南。这是我们可以期待的大量研究 – LionC
是的,我做过。但他们都似乎有点混乱。我想要的只是一个简单的方法。 – Andromise
[Notification](http://developer.android.com/reference/android/app/Notification.html)或[Toasts](http://developer.android.com/guide/topics/ui/notifiers/toasts。 html)? – Bishan