【问题标题】:Notification won't display?通知不会显示?
【发布时间】:2014-08-24 08:52:28
【问题描述】:

我有以下代码来显示带有自定义视图的通知:

private void showMediaPlayerNotification(){
        RemoteViews rm = new RemoteViews(getActivity().getPackageName(), R.layout.notif_media_player);
        Intent activity = new Intent(getActivity(), HomeActivity.class);
        PendingIntent pendingActivity = PendingIntent.getActivity(getActivity(), 0, activity,0);

        Notification notif =  new NotificationCompat.Builder(getActivity())
                              .setContent(rm).setContentIntent(pendingActivity)
                              .setOngoing(true)
                              .build();
        NotificationManager mgr = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(NOTIFICATION_ID, notif);

    }  

这是在一个片段内。该通知将在片段进入onStop() 时显示,并将在onResume()onDetach() 中删除。

但是,通知不会显示。

这里有什么问题?

这是在三星 Galaxy S3 上运行的。

<?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="wrap_content" 
    android:background="@android:color/black">

    <!-- Logo -->
    <ImageView android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:scaleType="fitXY"
        android:src="@drawable/chimp"
        android:layout_alignParentLeft="true" />

    <!-- Media Buttons -->
    <ImageButton android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_cancel"
        android:background="@android:color/transparent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_next"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/cancel"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_previous"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/next"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_pause"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/previous"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>
</RelativeLayout>

【问题讨论】:

  • 你能提供你的远程视图布局的内容吗?

标签: android android-notifications


【解决方案1】:

您还需要添加setSmallIcon(int drawable)。这应该可以解决问题。

【讨论】:

  • 像魅力一样工作!现在,我如何响应远程视图内的按钮? :)
  • 您需要在远程视图上使用setOnClickPendingIntent
  • 太棒了!我可以从我的 RemoteView 访问 logo ImageView 以在运行时更改图像吗?
  • 我不认为你可以这样改变它。我认为您需要使用正在更新的徽标图像视图来更新整个通知。
  • Google Play 音乐是如何做到的呢? :) 我确定他们使用的是自定义视图 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多