android Notification 状态栏通知使用示例
16lz
2021-01-26
ch7_notification.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是一个Notification使用示例"/> <Button android:id="@+id/bt_sendNotification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送Notification"/></LinearLayout>
ch7_second.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是一个通过Notification启动的Activity"/></LinearLayout>
NotificationActivity.java:
package com.example.ch7;import com.example.baseexample.R;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.location.GpsStatus.NmeaListener;import android.os.Bundle;import android.view.View;import android.widget.Button;public class NotificationActivity extends Activity {private Button bt_sendNotification = null;private Intent mIntent = null;private PendingIntent mPendingIntent = null;private Notification mNotification = null;private NotificationManager mNotificationManager = null;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.ch7_notification);bt_sendNotification = (Button)findViewById(R.id.bt_sendNotification);bt_sendNotification.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);mIntent = new Intent(NotificationActivity.this,SecondActivity.class);mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0);mNotification = new Notification();mNotification.icon = R.drawable.ic_launcher;mNotification.tickerText="实例";mNotification.defaults = Notification.DEFAULT_ALL;mNotification.flags = Notification.FLAG_INSISTENT;mNotification.setLatestEventInfo(NotificationActivity.this, "点击查看", "这是一个Notification示例", mPendingIntent);mNotificationManager.notify(1,mNotification);}});}}
SecondActivity.java :
package com.example.ch7;import com.example.baseexample.R;import android.app.Activity;import android.os.Bundle;public class SecondActivity extends Activity {public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.ch7_second);}}
<activity android:name="com.example.ch7.NotificationActivity" android:label="@string/app_name"></activity><activity android:name="com.example.ch7.SecondActivity" android:label="@string/app_name"></activity>
更多相关文章
- Android(安卓)密码 显示与隐藏功能实例
- [转]android jni层binder使用/进程通信实例
- 防sql注入
- AS:Android数据回传(简单实例源代码)
- android ui 更新 示例
- 访问网络的Json和图片
- Android(安卓)SAX解析xml为java
- Android中Sax解析与Dom解析xml文件
- 【黑马Android】(13)Linux操作系统/cron计划任务