android 自訂帶按鈕的Notification及點擊事件和伸縮通知欄

來源:互聯網
上載者:User

標籤:com   locale   putextra   manage   update   add   xtend   sys   broadcast   

1、自訂一個帶按鈕的Notification布局:layout_notification;

2、建立Notification:

  RemoteViews views = new RemoteViews(getPackageName(),R.layout.layout_nitification);  //自訂的布局視圖

  //按鈕點擊事件:

  PendingIntent homeIntent = PengdingIntent.getBroadcast(this,1,new Intent("action"),PengdingIntent.FLAG_UPDATE_CURRENT);

  views.setOnClickPendingIntent(R.id.btn,homeIntent);    //點擊的id,點擊事件

  //建立通知:

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext);

    mBuilder.setContent(views)    //設定布局

        .setOngoing(true)    //設定是否常駐,true為常駐

        .setSmallIcon(R.mipmap.ic_laucher)    //設定小表徵圖

        .setTicker("通知來了")        //設定提示

        .setPriority(Notification.PRIORITY_MAX)  //設定優先權

        .setWhen(System.currentTimeMillis())    //設定展示時間

        .setContentIntent(PendingIntent.getBroadcast(this,2,new Intent("action.view"),PengingIntent.FLAG_UPDATE_CURRENT);    //設定視圖點擊事件

    NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    mNotificationManager.notify(100,mBuilder.build())    //顯示通知: 當前notificationid,當前notification

3、建立NotificationBroadcast接收通知:

public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();    //動作
collapseStatusBar(context);        //收合通知欄
Intent i = new Intent(context,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    //必須添加,避免重複開啟
if (action.equals("com.xczl.smart.notification.home")){
i.putExtra("flag","home");    //傳值
context.startActivity(i);
}
} 

public void collapseStatusBar(Context context) {
    try {
Object statusBarManager = context.getSystemService("statusbar");
Method collapse;
if (Build.VERSION.SDK_INT <= 16) {
collapse = statusBarManager.getClass().getMethod("collapse");
} else {
collapse = statusBarManager.getClass().getMethod("collapsePanels");
}
collapse.invoke(statusBarManager);
} catch (Exception localException) {
localException.printStackTrace();
}
}

4、MainActivity接收訊息:

  覆寫 onNewIntent():

@Override
protected void onNewIntent(Intent intent) {
String extra = intent.getStringExtra("flag");
if (!TextUtils.isEmpty(extra)){
if (extra.equals("home")){
//接收到值的具體操作
}
}
}

5、配置清單:

   MainActivity:設定LanchMode為singleTask

   註冊Broadcast:

<receiver
android:name="com.xczl.smart.broadcast.NotificationBroadcast"
android:enabled="true">
<intent-filter>
<action android:name="action"/>
<action android:name="action.view"/>
</intent-filter>
</receiver>

     配置通知欄伸縮許可權:  

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

      

 

android 自訂帶按鈕的Notification及點擊事件和伸縮通知欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.