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.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
private NotificationManager manager;
private static final int ID=101;
private Notification n;
private RemoteViews views;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化通知管理者
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
public void sendMusicTZ(View v){
n = new Notification();
n.icon = R.drawable.logo;
//将音乐布局填充进来变成RemoteViews.
views = new RemoteViews(getPackageName(), R.layout.music_control_layout);
//views中有两个控制音乐播放的ImageView,我们希望点击这个两个ImageView的时候,能够触发一个延迟意图。
Intent intent_next = new Intent(this, NextActivity.class);
PendingIntent pi_next = PendingIntent.getActivity(this, 101, intent_next, 0);
views.setOnClickPendingIntent(R.id.imageView_next, pi_next);
Intent intent_pr = new Intent(this, PRActivity.class);
PendingIntent pi_pr = PendingIntent.getActivity(this, 102, intent_pr, 0);
views.setOnClickPendingIntent(R.id.imageView_pr, pi_pr);
n.contentView = views;
//contentIntent属性
Intent intent_sec = new Intent(this, SecActivity.class);
PendingIntent pi_sec = PendingIntent.getActivity(this, 100,intent_sec , 0);
n.contentIntent = pi_sec;
manager.notify(ID, n);
}
int progress=0;
public void updateTZ_ProgressBar(View v){
//1,先设置进度条的最大值。
//indeterminate非精确
views.setProgressBar(R.id.myProgressBar, 100, progress, false);
//开启线程更新进度条。
new Thread(){
public void run() {
while(true){
try {
Thread.sleep(100);
progress+=5;
views.setProgressBar(R.id.myProgressBar, 100, progress, false);
//同步到通知栏。
mHander.sendEmptyMessage(1);
if(progress>=100){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}.start();
}
private Handler mHander = new Handler(){
public void handleMessage(android.os.Message msg) {
manager.notify(ID, n);
};
};
public void updateTZ_PLAY(View v){
//点击当前按钮,播放张杰歌曲。
views.setImageViewResource(R.id.imageView_artist, R.drawable.zj);
views.setTextViewText(R.id.tv_songName, "直到世界尽头");
views.setImageViewResource(R.id.imageView_pr, R.drawable.pause);
//同步到通知栏。
manager.notify(ID, n);
}
public void sendTZ(View v){
//初始化通知。
Notification n = new Notification();
//icon提醒图标 tickerText提醒文字 标题 内容 时间 延迟意图。
n.icon = R.drawable.ic_launcher;
n.tickerText = "提醒文字";
n.when = System.currentTimeMillis();
/*n.setLatestEventInfo(this,
contentTitle,
contentText,
contentIntent);*/
Intent intent_sec = new Intent(this, SecActivity.class);;
//PendingIntent pi_sec = PendingIntent.getActivity(this, 100,intent_sec , PendingIntent.FLAG_ONE_SHOT);
PendingIntent pi_sec = PendingIntent.getActivity(this, 100,intent_sec , 0);
n.setLatestEventInfo(this,
"标题",
"内容",
pi_sec);
//如果通知只是第一次点击有效,我们还需要设置点击之后自动取消通知。
//n.flags = Notification.FLAG_AUTO_CANCEL;
//如果当前的通知是类似音乐播放控制那么点击多次都是有效,而且不会取消通知。
//利用管理者将通知挂在到通知栏。
manager.notify(ID, n);
}
public void cancleTZ(View v){
manager.cancel(ID);
}
}
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
import com.example.tz_notification.service.PlayService;
import com.example.tz_notification.service.PlayService.IPlay;
public class PlayActivity extends Activity {
private PlayService.IPlay iplay;
private MyServiceConnection conn;
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
iplay = (IPlay) binder;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
// 绑定PlayService.
initService();
}
private void initService() {
Intent intent = new Intent(this, PlayService.class);
// 首先启动服务。
startService(intent);
// 然后绑定服务。
Intent intent1 = new Intent(this, PlayService.class);
conn = new MyServiceConnection();
bindService(intent1, conn, Context.BIND_AUTO_CREATE);
}
public void play(View v) {
if(iplay!=null){
iplay.play();
}
}
public void next(View v) {
if(iplay!=null){
iplay.next();
}
}
public void pre(View v) {
if(iplay!=null){
iplay.pre();
}
}
public void pause(View v) {
if(iplay!=null){
iplay.pause();
}
}
public void resume(View v) {
if(iplay!=null){
iplay.resume();
}
}
// 完全退出
public void exit(View v) {
Intent intent = new Intent(this, PlayService.class);
stopService(intent);
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (conn != null) {
unbindService(conn);
}
}
}
import com.example.tz_notification.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;
public class PlayService extends Service{
private MyBinder mBinder;
private String[] names=new String[]{"Hello.mp3","zhangjie.mp3","chenzao.mp3"};
private int[] pics = new int[]{R.drawable.adele,R.drawable.zj,R.drawable.zy};
private MediaPlayer player;
private int currentIndex = 0;
private NotificationManager manager;
private Notification n;
private RemoteViews views;
private NextReceiver nextReceiver;
private PRReceiver prReceiver;
private static final String NEXT_FREQUENCE="com.tz.next";
private static final String P_R_FREQUENCE="com.tz.pr";
private static final int ID=200;
private boolean isServiceAlive = true;
private Handler mHander = new Handler(){
public void handleMessage(android.os.Message msg) {
manager.notify(ID, n);
};
};
@Override
public void onCreate() {
super.onCreate();
//初始化媒体播放器
player = new MediaPlayer();
try {
player.reset();
player.setDataSource("/mnt/sdcard/MyKuGou/"+names[currentIndex]);
player.prepare();
//player.start();
} catch (Exception e) {
}
//初始化通知。
initNotification();
//注册收音机
initReceiver();
//开启线程刷新进度条。
new UpdateProgressBar().start();
}
private class UpdateProgressBar extends Thread{
@Override
public void run() {
while(isServiceAlive){
try {
Thread.sleep(100);
if(player.isPlaying()){
//更新进度。
views.setProgressBar(R.id.myProgressBar, player.getDuration(), player.getCurrentPosition(), false);
mHander.sendEmptyMessage(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void initReceiver() {
//IntentFilter nextFilter = new IntentFilter("频率");
IntentFilter nextFilter = new IntentFilter(NEXT_FREQUENCE);
nextReceiver = new NextReceiver();
registerReceiver(nextReceiver, nextFilter);
IntentFilter prFilter = new IntentFilter(P_R_FREQUENCE);
prReceiver = new PRReceiver();
registerReceiver(prReceiver, prFilter);
}
private void initNotification() {
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
n = new Notification();
n.icon = R.drawable.logo;
views = new RemoteViews(getPackageName(),R.layout.music_control_layout);
//给views中的两个ImageView增加点击后的延迟意图。
Intent intent_next = new Intent(NEXT_FREQUENCE);
PendingIntent pi_next = PendingIntent.getBroadcast(this, 103, intent_next, 0);
views.setOnClickPendingIntent(R.id.imageView_next, pi_next);
Intent intent_pr = new Intent(P_R_FREQUENCE);
PendingIntent pi_pr = PendingIntent.getBroadcast(this, 104, intent_pr, 0);
views.setOnClickPendingIntent(R.id.imageView_pr, pi_pr);
n.contentView = views;
//发送通知
manager.notify(ID, n);
}
@Override
public IBinder onBind(Intent intent) {
mBinder= new MyBinder();
return mBinder;
}
public interface IPlay{
void play();
void next();
void pre();
void pause();
void resume();
}
private class MyBinder extends Binder implements IPlay{
@Override
public void play() {
try {
player.reset();
player.setDataSource("/mnt/sdcard/MyKuGou/"+names[currentIndex]);
player.prepare();
player.start();
//更新通知,变成当前歌手以及,当前的歌曲名称。
views.setImageViewResource(R.id.imageView_artist, pics[currentIndex]);
views.setTextViewText(R.id.tv_songName, names[currentIndex]);
views.setImageViewResource(R.id.imageView_pr, R.drawable.pause);
mHander.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void next() {
currentIndex++;
if(currentIndex==names.length){
currentIndex=0;
}
play();
}
@Override
public void pre() {
currentIndex--;
if(currentIndex==-1){
currentIndex=names.length-1;
}
play();
}
@Override
public void pause() {
// TODO Auto-generated method stub
player.pause();
//应该将图片变成继续。
views.setImageViewResource(R.id.imageView_pr, R.drawable.resume);
mHander.sendEmptyMessage(1);
}
@Override
public void resume() {
player.start();
//应该将图片变成暂停。
views.setImageViewResource(R.id.imageView_pr, R.drawable.pause);
mHander.sendEmptyMessage(1);
}
}
@Override
public void onDestroy() {
super.onDestroy();
//停止播放器。
player.reset();
player.release();
player = null;
//注销收音机。
unregisterReceiver(nextReceiver);
unregisterReceiver(prReceiver);
//Service销毁,更新线程停止工作。
isServiceAlive = false;
}
private class NextReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(PlayService.this, "播放下一首", 1).show();
if(mBinder!=null){
mBinder.next();
}
}
}
private class PRReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(PlayService.this, "暂停或者继续", 1).show();
if(player.isPlaying()){
mBinder.pause();
}else{
//设置数据源 设置准备
mBinder.resume();
}
}
}
}
整理自教程