您好,我正在我的 Android 应用程序中显示自定义通知。但问题是自定义通知未显示。我看到了很多帖子,但没有找到与我的问题相关的任何答案。
我找不到问题出在哪里。你能看一下这个吗?
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// display the notification
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.notificationIcon, Util.notificationBitmap);
contentView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
contentView.setTextViewText(R.id.notificationContent, notificationMessage);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_logo)
.setContentTitle(Util.notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText(notificationMessage);
mBuilder.setContent(contentView);
// build notification
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify((int)SystemClock.currentThreadTimeMillis(), mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
自定义通知.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/notificationIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/notificationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notificationIcon"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/notificationContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/notificationTitle"
android:layout_toRightOf="@id/notificationIcon"
android:textAppearance="?android:attr/textAppearanceSmall" /> …Run Code Online (Sandbox Code Playgroud) 到目前为止,我一直在RemoteViews使用remoteViews.setImageViewBitmap(). 它总体上运行良好。
但是一些用户遇到了问题,我认为这是在加载非常大的位图时。无论如何,我已经将位图缓存到本地存储,所以我的想法是改用setImageViewUri()其他地方的建议。
但我无法让它工作......我只是得到一个空白的小部件。以下代码段说明了我在做什么(省略了一些上下文,但希望留下足够的内容)...
// Bitmap bmp is fetched from elsewhere... and then...
FileOutputStream fos = context.openFileOutput("img.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
// ... later ...
// this works...
remoteViews.setImageViewBitmap(R.id.imageView, bmp);
// but this doesn't...
remoteViews.setImageViewUri(R.id.imageView, Uri.fromFile(new File(context.getFilesDir().getPath(), "img.png")));
Run Code Online (Sandbox Code Playgroud)
编辑
尝试FileProvider按照以下 CommonsWare 的建议使用...
显现:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
xml/file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
代码:
File imagePath = new File(context.getFilesDir(), ".");
File newFile = …Run Code Online (Sandbox Code Playgroud) 我在使用Application-Theme更改Background-Color时遇到问题.
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int iPrimaryColor = typedValue.data;
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int iPrimaryDarkColor = typedValue.data;
Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews smallContentView = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
nBuilder.setSmallIcon(R.drawable.not_icon)
.setOngoing(true)
.setContentTitle(getCurrentSong().getTitle())
.setContentIntent(notOpenOnClick);
Notification not = nBuilder.build();
smallContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
smallContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);
bigContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
bigContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);
setListeners(smallContentView);
setListeners(bigContentView);
not.contentView = smallContentView;
not.bigContentView = bigContentView;
if …Run Code Online (Sandbox Code Playgroud) 我用自定义视图做了一个通知,我需要在其中一个孩子上运行动画,但正如你所知RemoteViews,不允许直接访问所需的视图(start在其背景上调用方法),有谁知道如何访问视图或在其可绘制对象上调用提到的方法?
谢谢