android notification实例
16lz
2021-01-23
xml
两个Activity
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:text="Button" android:id="@+id/button1"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
两个Activity
package com.tcl.testnotifycation;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class TestNotificationActivity extends Activity {private NotificationManager mNotificationManager;private PendingIntent mPendingIntent;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 点击通知切换到Activity2Intent intent = new Intent(this, Activity2.class);mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);Button button = (Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Notification notification = new Notification();notification.icon = R.drawable.icon;// 显示图标notification.tickerText = "test...";// 点击通知用户的内容notification.defaults = Notification.DEFAULT_SOUND;// 默认声音// 通知时震动// notification.defaults = Notification.DEFAULT_VIBRATE;// 通知时屏幕发亮// notification.defaults = Notification.DEFAULT_LIGHTS;notification.setLatestEventInfo(TestNotificationActivity.this,"myNotification", "test test test", mPendingIntent);mNotificationManager.notify(0, notification);}});}}
package com.tcl.testnotifycation;import com.tcl.testnotifycation.R.layout;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;public class Activity2 extends Activity{public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); TextView textView = new TextView(this); textView.setText("this is test notifycation"); layout.addView(textView); setContentView(layout);}}
更多相关文章
- Android Wear Preview- 从通知上接收语音输入(Receiving Voice In
- Android Wear Preview- 为通知添加多个页面(Adding Pages to a No
- Android通知栏消息(基本文字通知)
- Android通知Notification学习 及 无法通知解决方案
- android 创建快捷方式图标
- Android图标靠界面的右边的实现
- 修改android桌面图标默认大小
- Android 5.1系统禁止通知状态栏下拉
- Android 创建悬停通知栏