自定义通知与系统通知的学习
自定义通知与系统通知的学习
通知这块比较简单,代码不多
[img]
[/img]
工程结构图:
[img]
[/img]
NotificationDemoActivity:
package com.amaker.notification;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;/** * @Title: NotificationDemoActivity.java * @Package com.amaker.notification * @Description: 通知控制类 * @author ZZL */public class NotificationDemoActivity extends Activity {private Button clearBtn ;private NotificationManager manager ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } /** * 初始化方法实现 */ private void init(){ //步骤一:取得系统服务 manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //步骤二: Notification notification = new Notification(R.drawable.notification, "收到小马通知测试", System.currentTimeMillis()); /** * 小马在这个地方写下为什么要在用到通知的时候要创建PendingIntent对象,是因为 * Notification可以与应用程序脱离,即便应用程序关闭,Notification仍然 * 会显示在状态栏中,当应用程序再次启动后,又可以重新控制这些Nofication消息, * 如清除或替换它们,因为才创建的此对象,更神奇的是这个对象由安卓系统本身维护哦,所以 * 在应用关闭后这个对象是不会被翻译掉的 */ //步骤三: Intent intent = new Intent(); intent.putExtra("Msg", "这是从Notification传递过来的信息"); intent.setClass(this, NotificationDemoTest.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0); //步骤四:setLatestEventInfo通过标准的方式将我们的通知设置到指定的View中 notification.setLatestEventInfo(this, "通知测试哦", "这是通知的主内容", contentIntent); //写下面这句话的时候大家注意下不要忘了加震动权限,不然没法调用硬件 //notification.defaults = Notification.DEFAULT_VIBRATE; //下面这句是把当前的通知设置永久保存的Notification,好暴力呀,吼吼 //notification.flags = Notification.FLAG_NO_CLEAR //下面这句是指:如果要让其它的软件检测到永久保存的通知时可以这样写 //Notification.flags = Nofication.FLAG_ONGOING_EVENT; /** * 在这一步需要指定标识Notification的唯一ID,这个ID必须相对于同一个 * NoficationManager对象是唯一的,否则就会覆盖相同ID的Notification */ //步骤五: manager.notify(R.drawable.notification, notification); clearBtn = (Button)findViewById(R.id.button1); clearBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { manager.cancel(R.drawable.notification); //清除所有通知: //manager.cancelAll(); } }); } }
package com.amaker.notification;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;import android.widget.RemoteViews;import android.widget.Toast;/** * @Title: NotificationDemoTest.java* @Package com.xiaoma.www.demo* @Description: 接收并弹出通知传递过来的信息* @author MZH*/public class NotificationDemoTest extends Activity {//声明变量private Button selfBtn;private NotificationManager manager ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.maintwo);init();}/** * 初始化方法实现 */private void init(){Intent i = this.getIntent();String msg = i.getStringExtra("Msg");if(!"".equals(msg)){Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "未接收到任何短信", Toast.LENGTH_SHORT).show();}selfBtn = (Button)findViewById(R.id.button2);selfBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {sendNotification();}});}private void sendNotification(){/** * 下面还是五个步骤,呵呵跟前面那个Activity是一样的,只是通知布局不同咯,用RemoteViews加载 */manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notification notification = new Notification(R.drawable.ic_launcher,"这是自定义通知的信息哦", System.currentTimeMillis());PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, getIntent(), 1);/** * RemoteViews这个类的官方文档解释为: * 很直接很简单的讲就是:从我们自己的XML文件中加载一个VIEW到通知中 */RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.notification);rViews.setTextViewText(R.id.textView, "更新自定义内容");notification.contentView = rViews;notification.contentIntent = pendingIntent;//notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的正文", pendingIntent);manager.notify(1, notification);}}
<?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:orientation="vertical" android:background="@drawable/background" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:text="清队当前通知" /></LinearLayout>
<?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:orientation="vertical" android:background="@drawable/background" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我查看自定义通知" android:textColor="#000000" /></LinearLayout>
<?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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义内容" android:textColor="#F00" android:textSize="20px" android:gravity="center" android:id="@+id/textView" /> <ImageButton android:id="@+id/imageBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/notification"/></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amaker.notification" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/xiaoma" android:label="@string/app_name" > <activity android:name=".NotificationDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NotificationDemoTest"></activity> </application></manifest>