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>