Notification通知实现自定义和带进度条

public class MyNotification extends Activity {
     private static final String MUSIC_PATH = "/mnt/sdcard/music/dzw.mp3";
     private Button mSendNotifyBtn;
     private NotificationManager mNotifyManager;
     private NotificationCompat.Builder mBuilder;
     private Handler mHandler = new Handler();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_mynotify_layout);
          mSendNotifyBtn = (Button) findViewById(R.id.notificationone);

          mSendNotifyBtn.setOnClickListener(new OnClickListener() {

               @Override
               public void onClick(View v) {
                    createCustomNotifiaction();
               }
          });
     }

     public void onBaseNotificationClick(View v) {
          createBaseNotification();
     }

     public void onBaseTedingNotificationClick(View v) {
          createBaseNotificationOne();
     }

     /**
     * 创建一个自定义Notification
     */
     public void createCustomNotifiaction() {
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    MyNotification.this);
          mBuilder.setSmallIcon(R.drawable.m1);
          mBuilder.setTicker("自定义通知,你有新消息");
          RemoteViews view = new RemoteViews(getPackageName(),
                    R.layout.notification_player_layout);
          mBuilder.setContent(view);

          Intent intent = new Intent(this, PlayerNotificationService.class);
          intent.putExtra("MUSIC_PATH", MUSIC_PATH);
          PendingIntent pendingIntent = PendingIntent.getService(this, 01,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
          view.setOnClickPendingIntent(R.id.playImageBtn, pendingIntent);

          Notification notification = mBuilder.build();
          int id = 29;

          NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          mNotifyManager.notify(id, notification);
     }

     /**
     * 创建一个带进度条的Notification
     */
     private void createProgressNotification() {
          mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          final int id = 001;
          // 第一步
          mBuilder = new NotificationCompat.Builder(MyNotification.this);
          mBuilder.setSmallIcon(R.drawable.m3);
          mBuilder.setContentTitle("图片下载");
          mBuilder.setContentText("正在下载中...");

          // mBuilder.setProgress(0, 0, false);
          new Thread() {
               public void run() {
                    int max = 100;
                    for (int i = 0; i < 100; i++) {
                         mBuilder.setProgress(max, i, true);
                         mNotifyManager.notify(id, mBuilder.build());
                         try {
                              Thread.sleep(200);
                         } catch (InterruptedException e) {
                              e.printStackTrace();
                         }
                    }
                    mHandler.post(new Runnable() {

                         @Override
                         public void run() {
                              mBuilder.setContentText("下载完成");
                              mNotifyManager.notify(id, mBuilder.build());
                         }
                    });
               };
          }.start();

          // 第三步 发布通知
          Notification notification = mBuilder.build();
          notification.flags = Notification.FLAG_AUTO_CANCEL;
          mNotifyManager.notify(id, notification);
     }
     /**
     * 创建一个普通的Notification
     */
     private void createBaseNotification() {
          // 第一步
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    MyNotification.this);
          mBuilder.setSmallIcon(R.drawable.m3);
          mBuilder.setContentTitle("通知标题");
          mBuilder.setContentText("通知内容,晚上来陪我");
          mBuilder.setTicker("收到一条新通知");
         
          //============BigView================
          Intent playIntent = new Intent(this,PlayerNotificationService.class);
          playIntent.setAction("action.scxh.android.service.PlayerNotificationService");
          playIntent.putExtra("MUSIC_PATH", MUSIC_PATH);
          PendingIntent playPengdingIntent = PendingIntent.getService(this,0, playIntent, 0);
         
         
          Intent playUIIntent = new Intent(this,PlayerUIActivity.class);
          PendingIntent playUiPengdingIntent = PendingIntent.getActivity(this,0, playUIIntent, 0);
          //================================
         
         
          // 第二步 设置通知点击行为
          Intent intent = new Intent(MyNotification.this, PlayerUIActivity.class);

          TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
          stackBuilder.addParentStack(PlayerUIActivity.class);
          stackBuilder.addNextIntent(intent);
          PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);

          mBuilder.setContentIntent(pendingIntent);
          mBuilder.setAutoCancel(true);

//          mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("BigViewText"));
          mBuilder.addAction(R.drawable.player_play, "播放", playPengdingIntent);
          mBuilder.addAction(R.drawable.player_pause,"暂停",playUiPengdingIntent);
          // 第三步 发布通知
          NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          int notificationId = 001;
          Notification notification = mBuilder.build();
          mNotifyManager.notify(notificationId, notification);
     }

     /**
     * 创建一个普通的Notification
     */
     private void createBaseNotificationOne() {
          // 第一步
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    MyNotification.this);
          mBuilder.setSmallIcon(R.drawable.m3);
          mBuilder.setContentTitle("通知标题");
          mBuilder.setContentText("通知内容,晚上来陪我");
          mBuilder.setTicker("收到一条新通知");
          mBuilder.setWhen(System.currentTimeMillis());
          // 第二步 设置通知点击行为
          Intent intent = new Intent(MyNotification.this, MyNotification.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

          PendingIntent pendingIntent = PendingIntent.getActivity(
                    MyNotification.this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
          mBuilder.setContentIntent(pendingIntent);
          mBuilder.setAutoCancel(true);

          // 第三步 发布通知
          NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          int notificationId = 001;
          Notification notification = mBuilder.build();
          mNotifyManager.notify(notificationId, notification);
     }
}

//跳转音乐播放界面
public class PlayerNotificationService extends Service {
     private MediaPlayer mMediaPlayer;
     private boolean isPause = false;

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
          String path = intent.getStringExtra("MUSIC_PATH");
          Logs.v("onStartCommand path  :"+path);
          playMusic(path);
          return super.onStartCommand(intent, flags, startId);
     }

     @Override
     public void onDestroy() {
          super.onDestroy();
          if (mMediaPlayer != null) {
               mMediaPlayer.release();
               mMediaPlayer = null;
          }
     }

     @Override
     public IBinder onBind(Intent intent) {
          return null;
     }
     public void playMusic(String path) {
          if (mMediaPlayer != null && isPause) {
               isPause = false;
               mMediaPlayer.start();
               return;
          }
          if (mMediaPlayer == null) {
               mMediaPlayer = new MediaPlayer();
          } else {
               mMediaPlayer.reset();
          }

          try {
               mMediaPlayer.setDataSource(path);

               mMediaPlayer.prepare();

          } catch (IllegalArgumentException e) {
               e.printStackTrace();
          } catch (SecurityException e) {
               e.printStackTrace();
          } catch (IllegalStateException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          }

          mMediaPlayer.start();

          mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

               @Override
               public void onCompletion(MediaPlayer mp) {
                    Intent intent = new Intent();
                    intent.setAction(Constances.ACTION_STOP);
                    sendBroadcast(intent);

               }
          });
     }

     public void pauseMusic() {
          if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
               mMediaPlayer.pause();
               isPause = true;
          }
     }
}

//XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/precImageBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:background="@drawable/bg_player_pre" />

        <ImageButton
            android:id="@+id/nextImageBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:background="@drawable/bg_player_next"
            android:text="3:40" />

        <ImageButton
            android:id="@+id/playImageBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@id/nextImageBtn"
            android:layout_toRightOf="@id/precImageBtn"
            android:background="@drawable/backgroud_play2"
            android:src="@drawable/player_play" />
    </RelativeLayout>

</LinearLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值