NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifyMgr.notify(R.string.app_name, notify);
import android.annotation.TargetApi; import android.app.Activity; 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.content.IntentFilter; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public class NotificationActivity extends Activity implements OnClickListener { private static final String TAG = "NotificationActivity"; private String mSong = "《两只老虎》"; private String PLAY_EVENT = ""; private boolean bPlay = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); Button btn_default = (Button) findViewById(R.id.btn_default); Button btn_custom = (Button) findViewById(R.id.btn_custom); Button btn_service = (Button) findViewById(R.id.btn_service); btn_default.setOnClickListener(this); btn_custom.setOnClickListener(this); btn_service.setOnClickListener(this); PLAY_EVENT = getResources().getString(R.string.play_event); } private void sendDefaultNotify() { Intent clickIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent cancelIntent = new Intent(this, MessengerActivity.class); PendingIntent deleteIntent = PendingIntent.getActivity(this, R.string.app_name, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(contentIntent) .setDeleteIntent(deleteIntent) .setUsesChronometer(true) .setProgress(100, 60, false) .setSubText("这里是副本") .setNumber(99) .setAutoCancel(false) .setSmallIcon(R.drawable.tt_s) .setTicker("提示文本") //.setWhen(System.currentTimeMillis()) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.tt_s)) .setContentTitle("标题文本") .setContentText("内容文本"); Notification notify = builder.build(); NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifyMgr.notify(R.string.app_name, notify); } private void sendCustomNotify(boolean is_play) { bPlay = !bPlay; Intent pIntent = new Intent(PLAY_EVENT); PendingIntent nIntent = PendingIntent.getBroadcast( this, R.string.app_name, pIntent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews widget_notify = new RemoteViews(getPackageName(), R.layout.widget_notify); if (is_play == true) { widget_notify.setTextViewText(R.id.btn_play, "暂停"); widget_notify.setTextViewText(R.id.tv_play, mSong+"正在播放"); widget_notify.setProgressBar(R.id.pb_play, 100, 10, false); widget_notify.setOnClickPendingIntent(R.id.btn_play, nIntent); widget_notify.setChronometer(R.id.chr_play, SystemClock.elapsedRealtime(), "%s", true); } else { widget_notify.setTextViewText(R.id.btn_play, "继续"); widget_notify.setTextViewText(R.id.tv_play, mSong+"暂停播放"); widget_notify.setProgressBar(R.id.pb_play, 100, 60, false); widget_notify.setChronometer(R.id.chr_play, SystemClock.elapsedRealtime(), "%s", false); } widget_notify.setOnClickPendingIntent(R.id.btn_play, nIntent); Intent intent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(contentIntent) .setContent(widget_notify) .setTicker(mSong) .setSmallIcon(R.drawable.tt_s); Notification notify = builder.build(); NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifyMgr.notify(R.string.app_name, notify); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_default) { sendDefaultNotify(); } else if (v.getId() == R.id.btn_custom) { sendCustomNotify(bPlay); } else if (v.getId() == R.id.btn_service) { Intent intent = new Intent(this, MusicService.class); intent.putExtra("is_play", bPlay); intent.putExtra("song", mSong); if (bPlay == true) { startService(intent); } else { stopService(intent); } bPlay = !bPlay; } } @Override public void onStart() { super.onStart(); notifyReceiver = new NotifyReceiver(); IntentFilter filter = new IntentFilter(PLAY_EVENT); registerReceiver(notifyReceiver, filter); } @Override public void onStop() { unregisterReceiver(notifyReceiver); super.onStop(); } private NotifyReceiver notifyReceiver; public class NotifyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { sendCustomNotify(bPlay); } } } }
import android.annotation.TargetApi; import android.app.Notification; 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.os.Binder; import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; import android.util.Log; import android.widget.RemoteViews; @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public class MusicService extends Service { private static final String TAG = "MusicService"; private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { public MusicService getService() { return MusicService.this; } } @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); return mBinder; } private String mSong; private String PAUSE_EVENT = ""; private boolean bPlay = true; private long mBaseTime; private long mPauseTime = 0; private int mProcess = 0; private Handler mHandler = new Handler(); private Runnable mPlay = new Runnable() { @Override public void run() { if (bPlay == true) { if (mProcess < 100) { mProcess+=2; } else { mProcess = 0; } mHandler.postDelayed(this, 200); } refresh(); } }; private void refresh() { Intent pIntent = new Intent(PAUSE_EVENT); PendingIntent nIntent = PendingIntent.getBroadcast( this, R.string.app_name, pIntent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews widget_notify = new RemoteViews(getPackageName(), R.layout.widget_notify); if (bPlay == true) { widget_notify.setTextViewText(R.id.btn_play, "暂停"); widget_notify.setTextViewText(R.id.tv_play, mSong+"正在播放"); widget_notify.setProgressBar(R.id.pb_play, 100, mProcess, false); widget_notify.setOnClickPendingIntent(R.id.btn_play, nIntent); widget_notify.setChronometer(R.id.chr_play, mBaseTime, "%s", true); } else { widget_notify.setTextViewText(R.id.btn_play, "继续"); widget_notify.setTextViewText(R.id.tv_play, mSong+"暂停播放"); widget_notify.setProgressBar(R.id.pb_play, 100, mProcess, false); widget_notify.setChronometer(R.id.chr_play, mBaseTime, "%s", false); } widget_notify.setOnClickPendingIntent(R.id.btn_play, nIntent); Intent intent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(contentIntent) .setContent(widget_notify) .setTicker(mSong) .setSmallIcon(R.drawable.tt_s); Notification notify = builder.build(); startForeground(1, notify); } @Override public int onStartCommand(Intent intent, int flags, int startid) { mBaseTime = SystemClock.elapsedRealtime(); bPlay = intent.getBooleanExtra("is_play", true); mSong = intent.getStringExtra("song"); Log.d(TAG, "bPlay="+bPlay+", mSong="+mSong); mHandler.postDelayed(mPlay, 200); return START_STICKY; } @Override public void onCreate() { PAUSE_EVENT = getResources().getString(R.string.pause_event); pauseReceiver = new PauseReceiver(); IntentFilter filter = new IntentFilter(PAUSE_EVENT); registerReceiver(pauseReceiver, filter); super.onCreate(); } @Override public void onDestroy() { unregisterReceiver(pauseReceiver); super.onDestroy(); } private PauseReceiver pauseReceiver; public class PauseReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { bPlay = !bPlay; if (bPlay == true) { mHandler.postDelayed(mPlay, 200); if (mPauseTime > 0) { long gap = SystemClock.elapsedRealtime() - mPauseTime; mBaseTime += gap; } } else { mPauseTime = SystemClock.elapsedRealtime(); } } } } }