现在的位置: 首页 > 综合 > 正文

Android Notification study

2014年11月12日 ⁄ 综合 ⁄ 共 4780字 ⁄ 字号 评论关闭
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="horizontal" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <ImageView android:id="@+id/image" android:layout_width="wrap_content"  
        android:layout_height="fill_parent" />  
  
    <TextView android:id="@+id/text" android:layout_width="wrap_content"  
        android:layout_toRightOf="@+id/image"  
        android:layout_height="wrap_content" android:textColor="#000" />  
          
    <ProgressBar android:id="@+id/progress_horizontal"  
        style="?android:attr/progressBarStyleHorizontal"   
        android:layout_below="@+id/text"  
        android:layout_toRightOf="@+id/image"  
        android:layout_width="fill_parent" android:layout_height="wrap_content"  
        android:max="100" android:progress="50"/>  
</RelativeLayout>   


package com.src.hero.org;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Notification To study
 * 
 * @author Hero
 * 
 */
public class NotificationActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	Button btn_ToNotif;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn_ToNotif = (Button) findViewById(R.id.btn_Notif);
		btn_ToNotif.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_Notif:
            Intent notif=new Intent();
            notif.setAction("com.src.hero.org.notifReveiver");
            notif.putExtra("message", "Hello World!");
            sendBroadcast(notif);
			break;
		default:
			break;
		}

	}
	
  
}

package com.src.hero.org;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class NotifReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String message=intent.getStringExtra("message");
		Log.v("tag", "---message:"+message);
		showCustomizeNotification(context);
	}
	
    //自定义显示的通知 ,创建RemoteView对象  
    private void showCustomizeNotification(Context context) {  
  
        CharSequence title = "I am a Notification";  
        int icon = R.drawable.collect_show;  
        long when = System.currentTimeMillis();  
        Notification noti = new Notification(icon, title, when + 10000);  
        noti.flags = Notification.FLAG_INSISTENT;  
          
        // 1、创建一个自定义的消息布局 view.xml  
        // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段  
        RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.notification);  
        remoteView.setImageViewResource(R.id.image, R.drawable.collect_show);  
        remoteView.setTextViewText(R.id.text , "自定义的NotificationView");  
        noti.contentView = remoteView;  
        // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)  
         
        //这儿点击后简单启动Settings模块  
        PendingIntent contentIntent = PendingIntent.getActivity  
                         (context, 0,new Intent("android.settings.SETTINGS"), 0);  
        noti.contentIntent = contentIntent;  
      
        NotificationManager mnotiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
        mnotiManager.notify(0, noti);  
  
    }  
  
    // 默认显示的的Notification  
    private void showDefaultNotification(Context context) {  
        // 定义Notication的各种属性  
         CharSequence title = "I am a Notification";  
        int icon = R.drawable.collect_show;  
        long when = System.currentTimeMillis();  
        Notification noti = new Notification(icon, title, when );  
        noti.flags = Notification.FLAG_INSISTENT;  
  
        // 创建一个通知  
        Notification mNotification = new Notification();  
  
        // 设置属性值  
        mNotification.icon = R.drawable.collect_show;  
        mNotification.tickerText = "来通知啦";  
        mNotification.when = System.currentTimeMillis(); // 立即发生此通知  
  
        // 带参数的构造函数,属性值如上  
        // Notification mNotification = = new Notification(R.drawable.icon,"NotificationTest", System.currentTimeMillis()));  
  
        // 添加声音效果  
        mNotification.defaults |= Notification.DEFAULT_SOUND;  
  
        // 添加震动,后来得知需要添加震动权限 : Virbate Permission  
        //mNotification.defaults |= Notification.DEFAULT_VIBRATE ;   
  
        //添加状态标志   
  
        //FLAG_AUTO_CANCEL          该通知能被状态栏的清除按钮给清除掉  
        //FLAG_NO_CLEAR                 该通知能被状态栏的清除按钮给清除掉  
        //FLAG_ONGOING_EVENT      通知放置在正在运行  
        //FLAG_INSISTENT                通知的音乐效果一直播放  
        mNotification.flags = Notification.FLAG_AUTO_CANCEL ;  
  
        //将该通知显示为默认View  
        PendingIntent contentIntent = PendingIntent.getActivity  
                           (context, 0,new Intent("android.settings.SETTINGS"), 0);  
        mNotification.setLatestEventInfo(context, "系统默认的view", "我其实想做个好人",contentIntent);  
          
        // 设置setLatestEventInfo方法,如果不设置会App报错异常  
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
          
        //注册此通知   
        // 如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息 ,比如tickerText 等  
        mNotificationManager.notify(2, mNotification);  
  
    }  
      
    private void removeNotification(Context context)  
    {  
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
        // 取消的只是当前Context的Notification  
        mNotificationManager.cancel(2);  
    }

}

抱歉!评论已关闭.