0

这是我用来为任何事件设置通知的代码。但我的要求是,除了文字之外,我还想在通知面板中显示一张图片,它不是徽标,是一张完整的图片(或可能被裁剪)。如何修改此代码以完成此工作。

其次,我的通知可以正常工作,但是当要显示的文本太多时,它不会扩展。有什么方法可以使通知可扩展。

第三,我的代码的某些部分,您会看到,这些命令已被弃用(当我启动通知时以及在notification.setLatestInfo()时)。那么,正确的编写方法是什么,如果我编写它,它是否也适用于较低版本?提前致谢。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ok = (Button) findViewById(R.id.dones);
    et = (EditText) findViewById(R.id.ets);


    ok.setOnClickListener(MainActivity.this);

    counter = getSharedPreferences("count", 0);

    notificationId = counter.getInt("val", 0);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
    String Text = et.getText().toString();

    if(Text.equals("")){
        Toast.makeText(getApplicationContext(), "You Don't have anything to clip", Toast.LENGTH_SHORT).show();
    }
    else{
        notification = new Notification(R.drawable.ic_launcher, "You Got Things ToDo!", System.currentTimeMillis());
        notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
        notification.setLatestEventInfo(getApplicationContext(), "I want to ..", Text, pending);

        notificationmanager.notify(notificationId, notification);
        notificationId++;

        notification.flags |= Notification.FLAG_NO_CLEAR;

    }
}
4

2 回答 2

2

你应该检查一下Notification.Builder,特别是setStyle()

http://developer.android.com/reference/android/app/Notification.Builder.html

然后Notification.BigPictureStyle应该允许您显示您想要的图像:

http://developer.android.com/reference/android/app/Notification.BigPictureStyle.html

使用代码示例进行编辑,因为您也在处理位图,所以我添加了一个 AsyncTask 以便处理不会发生在 UI 线程上:

else{
    new AsyncTask<Void, Void, Void>(){

        public Void doInBackround(Void... args){
            PendingIntent pending = PendingIntent.getActivity(arg0.getContext(), 0, new Intent(arg0.getContext(),MainActivity.class), 0);

            Bitmap bmp = BimapFactory.decodeResource(arg0.getContext().getResources(), R.id.my_img);

            notification = new NotificationCompat.Builder(arg0.getContext())
             .setContentTitle("I want to...")
             .setContentText(Text)
             .setContentIntent(pending)
             .setSmallIcon(R.drawable.ic_launcher)
             .setStyle(new NotificationCompat.BigPictureStyle()
                 .bigPicture(bmp))
             .build();
            notificationmanager = (NotificationManager)arg0.getContext().getSystemService(Context.NOTIFICATION_SERVICE);

            notificationmanager.notify(notificationId, notification);
            notificationId++;

        }
    }.execute();
}
于 2014-01-04T16:36:49.937 回答
1

您可以在自定义通知布局中使用 RemoteViews 执行此操作。如果您有具体问题,请随时发布。

于 2014-01-04T16:27:18.977 回答