我检查了以下问题,据我所知,我只能使用与 RemoteViews 兼容的视图,我使用的是可以工作的 Linear 和 ImageView,每次触发 Notification 方法时,我都会收到“无法展开远程视图”错误。
我的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon2" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon4" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AppIcon6" />
</LinearLayout>
</LinearLayout>
发送通知代码:
private void SendNotifyShortcuts(){
UpdatePreferences();
nBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setOngoing(true);
remoteView = new RemoteViews(getPackageName(), R.layout.notification_layout);
if (App_1 == null) {
remoteView.setImageViewResource(R.id.AppIcon1, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_1, 0);
icon = getPackageManager().getApplicationIcon(App_1);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon1, AppIcon);
}
if (App_2 == null) {
remoteView.setImageViewResource(R.id.AppIcon2, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_2, 0);
icon = getPackageManager().getApplicationIcon(App_2);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon2, AppIcon);
}
if (App_3 == null) {
remoteView.setImageViewResource(R.id.AppIcon3, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_3, 0);
icon = getPackageManager().getApplicationIcon(App_3);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon3, AppIcon);
}
if (App_4 == null) {
remoteView.setImageViewResource(R.id.AppIcon4, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_4, 0);
icon = getPackageManager().getApplicationIcon(App_4);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon4, AppIcon);
}
if (App_5 == null) {
remoteView.setImageViewResource(R.id.AppIcon5, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_5, 0);
icon = getPackageManager().getApplicationIcon(App_5);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon5, AppIcon);
}
if (App_6 == null) {
remoteView.setImageViewResource(R.id.AppIcon6, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_6, 0);
icon = getPackageManager().getApplicationIcon(App_6);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon6, AppIcon);
}
setListeners(remoteView, this);
nBuilder.setContent(remoteView);
nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOFIY_ID, nBuilder.build());
}
public static void CancelNotify(){
try {
nManager.cancel(NOFIY_ID);
} catch (Exception e){
e.printStackTrace();
}
}
public static void setListeners(RemoteViews view, Context context){
Intent App1 = new Intent("App1");
PendingIntent App1P = PendingIntent.getBroadcast(context, 0, App1, 0);
view.setOnClickPendingIntent(R.id.AppIcon1, App1P);
Intent App2 = new Intent("App2");
PendingIntent App2P = PendingIntent.getBroadcast(context, 1, App2, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App2P);
Intent App3 = new Intent("App3");
PendingIntent App3P = PendingIntent.getBroadcast(context, 3, App3, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App3P);
Intent App4 = new Intent("App4");
PendingIntent App4P = PendingIntent.getBroadcast(context, 4, App4, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App4P);
Intent App5 = new Intent("App5");
PendingIntent App5P = PendingIntent.getBroadcast(context, 5, App5, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App5P);
Intent App6 = new Intent("App6");
PendingIntent App6P = PendingIntent.getBroadcast(context, 6, App6, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App6P);
}
有什么解决办法?而且我在问题中看到我可以在通知中制作 ListView,这是怎么回事?因为没有官方文档或指南可以做到这一点。