远程视图中具有动态布局的自定义通知

时间:2014-03-12 09:24:07

标签: android android-notifications

我在远程视图中使用动态布局而不是xml布局创建自定义通知,但最终会出现以下错误。

03-12 14:42:59.907: E/AndroidRuntime(2399): android.app.RemoteServiceException: Bad notification posted from package com.example.notificationexample: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.example.notificationexample id=0 tag=null score=0 notn=Notification(pri=0 contentView=com.example.notificationexample/0x1e vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null]))
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.os.Looper.loop(Looper.java:137)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at android.app.ActivityThread.main(ActivityThread.java:4905)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 14:42:59.907: E/AndroidRuntime(2399):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

我看到很多问题几乎同样的问题,但他们在布局中使用xml。我正在使用动态布局。这是我的代码:

public void CustomNotification() {


        //creating Custom Layout
        RelativeLayout parent_layout=new RelativeLayout(this);
        RelativeLayout.LayoutParams rlparent = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        parent_layout.setLayoutParams(rlparent);
        parent_layout.setBackgroundColor(Color.WHITE);
        parent_layout.setId(30);

        //creating image
        ImageView iv=new ImageView(this);
        RelativeLayout.LayoutParams rl_iv=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        iv.setId(31);
        iv.setImageResource(R.drawable.androidhappy);
        iv.setLayoutParams(rl_iv);
        parent_layout.addView(iv);

        //creataing text view "title"

         TextView title = new TextView(this);
         RelativeLayout.LayoutParams title_Param = new RelativeLayout.LayoutParams(
                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                      RelativeLayout.LayoutParams.WRAP_CONTENT);
         title_Param.addRule(RelativeLayout.RIGHT_OF,iv.getId());
         title_Param.addRule(RelativeLayout.ALIGN_LEFT);
        // title.setText("Title");
         title.setEllipsize(TextUtils.TruncateAt.END);
         title.setPadding(6, 5, 0, 0);
         title.setSingleLine(true);
         title.setTextColor(Color.BLACK);
         title.setTextSize(12);
        title.setText("Good Morning");
         title.setId(11);
          title.setLayoutParams(title_Param);
         parent_layout.addView(title);


        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),parent_layout.getId());
      //    RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.orignal_customnotification);

        // Set Notification Title
        String strtitle = getString(R.string.customnotificationtitle);
        // Set Notification Text
        String strtext = getString(R.string.customnotificationtext);

        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this)
        // Set Icon
                .setSmallIcon(R.drawable.logosmall)
                // Set Ticker Message
                .setTicker("My Ticker text...")
                // Dismiss Notification
                .setAutoCancel(true)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // Set RemoteViews into Notification
                .setContent(remoteViews);


        remoteViews.setImageViewResource(iv.getId(),R.drawable.androidhappy);
        remoteViews.setTextViewText(title.getId(), "Good Morning..");


// Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());


    }

1 个答案:

答案 0 :(得分:1)

你想做什么是不可能的;实时View对象不能与通知一起使用; RemoteViews是唯一支持Notification.contentView的类型。如果你查看API docs,你可以看到创建一个文件的唯一方法是引用一个布局xml文件,虽然你有一个,你可以用很多不同的方式修改它,包括添加其他{{1}到布局。