通知
通知 特殊 进度条 builder.setProgress(100,50,true); 自定义 builder.setContent(remoteViews);
自定义通知
NotificationManager manager = ( NotificationManager) getSystemService ( NOTIFICATION_SERVICE) ;
Notification. Builder builder = new Notification. Builder ( MainActivity. this ) ;
builder. setSmallIcon ( R. mipmap. yinle) ;
RemoteViews remoteViews = new RemoteViews ( getPackageName ( ) , R. layout. tong) ;
remoteViews. setTextViewText ( R. id. te_1, "浩" ) ;
remoteViews. setTextViewText ( R. id. te_2, "新歌" ) ;
remoteViews. setImageViewBitmap ( R. id. img_1, bitmap) ;
builder. setContent ( remoteViews) ;
manager. notify ( 2 , builder. build ( ) ) ;
普通通知
NotificationManager manager = ( NotificationManager) getSystemService ( NOTIFICATION_SERVICE) ;
Notification. Builder builder = new Notification. Builder ( this ) ;
builder. setSmallIcon ( R. mipmap. ic_launcher_round) ;
builder. setContentText ( "内容" ) ;
builder. setContentTitle ( "标题" ) ;
builder. setContentInfo ( "附加信息" ) ;
builder. setTicker ( "提示消息" ) ;
builder. setAutoCancel ( true ) ;
builder. setDefaults ( Notification. DEFAULT_ALL) ;
Intent intent = new Intent ( MainActivity. this , Main2Activity. class ) ;
PendingIntent pendingIntent = PendingIntent. getActivity ( MainActivity. this , 101 , intent, PendingIntent. FLAG_ONE_SHOT) ;
builder. setContentIntent ( pendingIntent) ;
manager. notify ( 1 , builder. build ( ) ) ;
进度条通知
final NotificationManager manager = ( NotificationManager) getSystemService ( NOTIFICATION_SERVICE) ;
final Notification. Builder builder = new Notification. Builder ( MainActivity. this ) ;
builder. setSmallIcon ( R. mipmap. one) ;
manager. notify ( 3 , builder. build ( ) ) ;
final Timer timer = new Timer ( ) ;
timer. schedule ( new TimerTask ( ) {
int pro = 0 ;
@Override
public void run ( ) {
builder. setProgress ( 100 , pro+= 20 , false ) ;
manager. notify ( 3 , builder. build ( ) ) ;
if ( pro== 100 ) {
builder. setProgress ( 100 , 100 , true ) ;
manager. notify ( 3 , builder. build ( ) ) ;
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
}
timer. cancel ( ) ;
manager. cancel ( 3 ) ;
}
}
} , 0 , 1000 ) ;
双击退出
long flag = 0 ;
@Override
public boolean onKeyDown ( int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent. KEYCODE_BACK) {
long nowTime = System. currentTimeMillis ( ) ;
if ( ( nowTime- flag) <= 1000 ) {
flag = nowTime;
finish ( ) ;
} else {
flag = nowTime;
Toast. makeText ( this , "在点击一次退出!" , Toast. LENGTH_SHORT) . show ( ) ;
return false ;
}
}
return super . onKeyDown ( keyCode, event) ;
}