Android Notification 小Demo

今天写了一个关于android中通知消息的demo,特此记录。可呼吸灯始终没效果,如果有知道的请赐教呀。

其中包括:

  1. 正常通知
  2. 声音通知
  3. 呼吸灯通知
  4. 震动通知
  5. 声音+震动+呼吸灯通知
  6. 自己简单实现的一个显示进度条的通知栏
其中点击通知栏会打开一个显示内容的Activity,通过PendingIntent实现。
代码如下:

package com.wqy.notificationdemo;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.wqy.notificationdemo.utils.Constants;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        private Button normal;
        private Button sound;
        private Button light;
        private Button vibrate;
        private Button all;
        private Button custom;

        private NotificationManager notiManager;
        private Notification notification;

        private Runnable runnable;

        private int progress = 0;
        public PlaceholderFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            notiManager = (NotificationManager)this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            initViews(rootView);
            return rootView;
        }

        private void initViews(View rootView){
            normal = (Button)rootView.findViewById(R.id.normal);
            sound = (Button)rootView.findViewById(R.id.sound);
            light = (Button)rootView.findViewById(R.id.light);
            vibrate = (Button)rootView.findViewById(R.id.vibrate);
            all = (Button)rootView.findViewById(R.id.all);
            custom = (Button)rootView.findViewById(R.id.custom);

            normal.setOnClickListener(new ButtonClickListener());
            sound.setOnClickListener(new ButtonClickListener());
            light.setOnClickListener(new ButtonClickListener());
            vibrate.setOnClickListener(new ButtonClickListener());
            all.setOnClickListener(new ButtonClickListener());
            custom.setOnClickListener(new ButtonClickListener());
        }

        class ButtonClickListener implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                int icon;
                String tickerText;
                long when;
                Intent intent;
                Bundle bundle;
                final PendingIntent pendingIntent;
                switch(v.getId()){
                    case R.id.normal:
                        icon = R.drawable.ic_launcher;
                        tickerText = "Normal Notification";
                        when = System.currentTimeMillis();
                        notification = new Notification(icon,tickerText,when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,"This is a notification from wqy, and it comes with a normal notification!");
                        intent.putExtra(Constants.BUNDLE,bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_ONE_SHOT);
                        notification.setLatestEventInfo(getActivity(),"Normal","This is a notification from wqy",pendingIntent);
                        notification.flags = Notification.FLAG_AUTO_CANCEL;
                        notiManager.notify(Constants.NORMAL_NOTI,notification);
                        break;
                    case R.id.sound:
                        icon = R.drawable.ic_launcher;
                        tickerText = "This is a notification from wqy, and it comes with a sound notification!";
                        when = System.currentTimeMillis();
                        notification = new Notification(icon,tickerText,when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,tickerText);
                        intent.putExtra(Constants.BUNDLE,bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_ONE_SHOT);
                        notification.setLatestEventInfo(getActivity(),"Sound","This is a notification from wqy",pendingIntent);
                        notification.flags = Notification.FLAG_AUTO_CANCEL;
                        notification.defaults = Notification.DEFAULT_SOUND;
                        notiManager.notify(Constants.SOUND_NOTI,notification);
                        break;
                    case R.id.light:
                        icon = R.drawable.ic_launcher;
                        tickerText = "This is a notification from wqy, and it comes with a light notification!";
                        when = System.currentTimeMillis();
                        notification = new Notification(icon,tickerText,when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,tickerText);
                        intent.putExtra(Constants.BUNDLE, bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_ONE_SHOT);
                        notification.setLatestEventInfo(getActivity(),"Light","This is a notification from wqy",pendingIntent);
                        notification.flags = Notification.FLAG_AUTO_CANCEL;
                        notification.ledARGB = Color.BLUE;
                        notification.ledOffMS = 100;
                        notification.ledOnMS = 1000;
                        notification.flags = notification.flags|Notification.FLAG_SHOW_LIGHTS;
//                        notification.defaults = Notification.DEFAULT_LIGHTS;
                        notiManager.notify(Constants.LIGHT_NOTI,notification);
                        break;
                    case R.id.vibrate:
                        icon = R.drawable.ic_launcher;
                        tickerText = "This is a notification from wqy, and it comes with a vibrate notification!";
                        when = System.currentTimeMillis();
                        notification = new Notification(icon,tickerText,when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,tickerText);
                        intent.putExtra(Constants.BUNDLE,bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_ONE_SHOT);
                        notification.setLatestEventInfo(getActivity(),"Vibrate","This is a notification from wqy",pendingIntent);
                        notification.flags = Notification.FLAG_AUTO_CANCEL;
                        notification.defaults = Notification.DEFAULT_VIBRATE;
                        notiManager.notify(Constants.VIBRATE_NOTI,notification);
                        break;
                    case R.id.all:
                        icon = R.drawable.ic_launcher;
                        tickerText = "This is a notification from wqy, and it comes with a all notification!";
                        when = System.currentTimeMillis();
                        Notification.Builder builder = new Notification.Builder(getActivity());
                        builder.setSmallIcon(R.drawable.ic_launcher);
                        builder.setTicker(tickerText);
                        builder.setWhen(when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,tickerText);
                        intent.putExtra(Constants.BUNDLE, bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_ONE_SHOT);
                        builder.setContentTitle("All").
                                setContentInfo("This is a notification from wqy").
                                setContentIntent(pendingIntent);
                        builder.setVibrate(new long[]{200, 200, 200, 200}).
                                setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
                                setLights(Color.BLUE, 1000, 1000);
//                        notification = new Notification(icon,tickerText,when);
//
//                        notification.setLatestEventInfo(getActivity(),"All","This is a notification from wqy",pendingIntent);
//                        notification.flags = Notification.FLAG_AUTO_CANCEL;
//                        notification.defaults = Notification.DEFAULT_ALL;
                        notification = builder.getNotification();
                        notification.flags = Notification.FLAG_AUTO_CANCEL|Notification.FLAG_SHOW_LIGHTS;
                        notiManager.notify(Constants.ALL_NOTI,notification);
                        break;
                    case R.id.custom:
                        progress = 0;
                        icon = R.drawable.ic_launcher;
                        tickerText = "This is a notification from wqy, and it has a custom view!";
                        when = System.currentTimeMillis();
                        notification = new Notification(icon,tickerText,when);
                        intent = new Intent(getActivity(),MessageActivity.class);
                        bundle = new Bundle();
                        bundle.putString(Constants.MESSAGE,tickerText);
                        intent.putExtra(Constants.BUNDLE, bundle);
                        pendingIntent = PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
                        notification.setLatestEventInfo(getActivity(),"All","This is a notification from wqy",pendingIntent);
                        notification.flags = Notification.FLAG_AUTO_CANCEL;
                        RemoteViews remoteViews = new RemoteViews(getActivity().getPackageName(),R.layout.notification_view);
                        final Handler handler = new Handler(){

                            @Override
                            public void handleMessage(Message msg) {
                                if(msg.what==100){
                                    notification.contentView.setProgressBar(R.id.progress_bar,100,msg.what,false);
                                    notification.contentView.setTextViewText(R.id.noti_info,"Upgrade Completed, Click to Open!");
                                    notiManager.notify(Constants.CUSTOM_NOTI,notification);
                                    removeCallbacks(runnable);
                                }else{
                                    notification.contentView.setProgressBar(R.id.progress_bar,100,msg.what,false);
                                    notification.contentView.setTextViewText(R.id.noti_info,msg.what+"%");
                                    notiManager.notify(Constants.CUSTOM_NOTI,notification);
                                }
                            }
                        };
                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                while(progress<100){
                                    Message msg = handler.obtainMessage();
                                    progress += 10;
                                    msg.what = progress;
                                    if(msg.what>=100){
                                        msg.what = 100;
                                    }
                                    handler.sendMessage(msg);
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        };
                        notification.contentView = remoteViews;
                        notification.contentView.setProgressBar(R.id.progress_bar,100,0,false);
                        notification.contentView.setTextViewText(R.id.noti_info,progress+"%");
                        notification.contentIntent = pendingIntent;
                        notiManager.notify(Constants.CUSTOM_NOTI,notification);
                        new Thread(runnable).start();
                        break;

                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值