一、Notification的按钮
相关代码:
/** * 显示或者刷新通知 */ private void showNotification() { notification = new Notification(); notification.flags = Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_NO_CLEAR; notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; notification.icon = R.drawable.ic_launcher; notification.tickerText = "点击打开应用"; RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notifi); String allReslut = BLTPreference.readPreference(this, ConstantPhoneInfo.SP_PREDICTION_RESULT, ""); ArrayList<Programe> list = new ArrayList<>(); String r[] = allReslut.split(","); for (int i = 0; i < r.length; i++) { if (!r[i].trim().equals("")) { Programe p = AppInfoUtil.getProgramDetailByPackageName(this, r[i].trim()); list.add(p); } } Intent buttonIntent = new Intent(ACTION_BUTTON); if (list.size() > 0) { rv.setImageViewBitmap(R.id.iv1, BLTViewUtil.drawableToBitmap(list.get(0).icon)); buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_IV1_ID); buttonIntent.putExtra(INTENT_PACKAGE_TAG, list.get(0).packageName); // 这里加了广播,所及INTENT的必须用getBroadcast方法,注意第二个参数requestCode需要是不同的int值,不然会重置buttonIntent的内容 PendingIntent intent_iv1 = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.iv1, intent_iv1); } if (list.size() > 1) { rv.setImageViewBitmap(R.id.iv2, BLTViewUtil.drawableToBitmap(list.get(1).icon)); buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_IV2_ID); buttonIntent.putExtra(INTENT_PACKAGE_TAG, list.get(1).packageName); PendingIntent intent_iv2 = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.iv2, intent_iv2); } if (list.size() > 2) { rv.setImageViewBitmap(R.id.iv3, BLTViewUtil.drawableToBitmap(list.get(2).icon)); buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_IV3_ID); buttonIntent.putExtra(INTENT_PACKAGE_TAG, list.get(2).packageName); PendingIntent intent_iv3 = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.iv3, intent_iv3); } if (list.size() > 3) { rv.setImageViewBitmap(R.id.iv4, BLTViewUtil.drawableToBitmap(list.get(3).icon)); buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_IV4_ID); buttonIntent.putExtra(INTENT_PACKAGE_TAG, list.get(3).packageName); PendingIntent intent_iv4 = PendingIntent.getBroadcast(this, 4, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.iv4, intent_iv4); } notification.contentView = rv; if (list.size() == 0) { // 如果没有可预测的数据,则点击进入本应用 rv.setViewVisibility(R.id.iv, View.VISIBLE); rv.setViewVisibility(R.id.ll, View.GONE); Intent i1 = new Intent(this, ActHome.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i1, PendingIntent.FLAG_UPDATE_CURRENT); notification.contentIntent = contentIntent; } else { rv.setViewVisibility(R.id.iv, View.GONE); rv.setViewVisibility(R.id.ll, View.VISIBLE); } this.startForeground(1, notification); } /** * 初始化广播,需要在Service或者Activity开始的时候就调用 */ public void initButtonReceiver() { bReceiver = new ButtonBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_BUTTON); registerReceiver(bReceiver, intentFilter); } public ButtonBroadcastReceiver bReceiver; public final static String ACTION_BUTTON = "com.notifications.intent.action.ButtonClick"; public final static String INTENT_BUTTONID_TAG = "ButtonId"; public final static String INTENT_PACKAGE_TAG = "packageName"; public final static int BUTTON_IV1_ID = 1; public final static int BUTTON_IV2_ID = 2; public final static int BUTTON_IV3_ID = 3; public final static int BUTTON_IV4_ID = 4; /** * 按钮点击广播 * */ public class ButtonBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ACTION_BUTTON)) { collapseStatusBar(ServiceProcess.this); Intent i1 = new Intent(); int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0); String packageName = intent.getStringExtra(INTENT_PACKAGE_TAG); System.out.println(packageName + " " + buttonId); PackageManager packageManager = ServiceProcess.this.getPackageManager(); i1 = packageManager.getLaunchIntentForPackage(packageName); i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP); ServiceProcess.this.startActivity(i1); // // 对不同的按钮做不同的事情 // switch (buttonId) { // case BUTTON_IV1_ID: // BLTLog.v("BUTTON_IV1_ID", "BUTTON_IV1_ID"); // // break; // } } } } /** * 关闭下拉通知栏 * * 需要添加权限:<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> * * @param context */ public static 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(); } }
二、点击按钮收回系统通知栏
注意:需要在Manifest中添加权限:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
代码如下:使用反射
/** * 关闭下拉通知栏 * * 需要添加权限:<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> * * @param context */ public static 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(); } }