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);}}


AndroidManifest.xml 中application节点中添加 :

<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>



更多相关文章

  1. Android(安卓)密码 显示与隐藏功能实例
  2. [转]android jni层binder使用/进程通信实例
  3. 防sql注入
  4. AS:Android数据回传(简单实例源代码)
  5. android ui 更新 示例
  6. 访问网络的Json和图片
  7. Android(安卓)SAX解析xml为java
  8. Android中Sax解析与Dom解析xml文件
  9. 【黑马Android】(13)Linux操作系统/cron计划任务

随机推荐

  1. Android涂鸦
  2. Android Gallery3D
  3. Android studio 点击跳转WedView
  4. android 单个模块编译
  5. Android 动态更改Shape(GradientDrawable)
  6. Android 8.0 鍓嶅彴鏈嶅姟
  7. android ListView添加checkbox注意事项
  8. Android学习札记23:LruCache
  9. android SQLite的CRUD
  10. Android(安卓)ScrollView用法