因为有需求要做非系统通知,所以小马找个时间干脆一起学习了系统默认的通知与自定义通知的实现,吼吼,虽然简单,但开心呀,不多讲,老规矩,先看效果再来看代码:
一:应用刚启动时:
![]()
二:查看系统默认接收到通知时的效果图:
![]()
三:自定义通知小提示效果图:
![]()
四:自定义通知布局与系统默认布局对比
![]()
有了效果图后再后代码就简单多了,直接看看代码,小马就直接在源代码里面加了注释,有不妥之处还请朋友们提出来 :
- package com.xiaoma.www.demo;
-
- 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 NotificationDemoActivity extends Activity {
-
- private Button clearBtn ;
- private NotificationManager manager ;
-
- @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());
-
-
-
-
-
-
-
-
-
- Intent intent = new Intent();
- intent.putExtra("Msg", "这是从Notification传递过来的信息");
- intent.setClass(this, NotificationDemoTest.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
-
-
- notification.setLatestEventInfo(this, "通知测试哦", "这是通知的主内容", contentIntent);
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
-
-
- }
- });
-
- }
-
- }
再来看下接收上面这个通知通过Intent发给第二个Activity的代码,虽然很简单,但有这个后,大家就可以随便更改系统级的通知咯,不用老是用系统很死板很统一的布局,写出自己的个性啦,吼吼,加油加油,看代码了:
- package com.xiaoma.www.demo;
-
- 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 ;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.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() {
- @Override
- public 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);
-
- }
-
-
- }
这个代码虽然很简单,但小马想通过自己的积累,哪怕每天积累一点点知识点,足矣,吼吼,记录下自己成长的过程,如果朋友们对我的小DEMO有什么好的建议请直接跟小马讲,一定向各位虚心学习,先谢谢啦,最后 ,老样子,这个小DEMO的源码小马还是会放到附件中去,希望朋友们发现问题请直接指出,谢谢