在Android中自定义通知区域

BST Kaal

RemoteViews用来创建自定义通知。有什么方法可以自定义通知区域的特别高度... ??? 我不想使用BigViewcoz,我需要将其用于小于11的API。

的快照是:

在此处输入图片说明 在此处输入图片说明

这是我的代码:

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notificationmain);

        Button bnotify = (Button) findViewById(R.id.notification);
        Button bcustomnotify = (Button) findViewById(R.id.customnotification);

        // Click For Default Notification       
        bnotify.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Notification();
            }
        });

        // Click For Custom Notification
        bcustomnotify.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                CustomNotification();
            }
        });

    }

    // Default Notification
    public void Notification() {
        String strtitle = getString(R.string.notificationtitle);
        String strtext = getString(R.string.notificationtext);
        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logosmall)
                .setTicker(getString(R.string.notificationticker))
                .setContentTitle(getString(R.string.notificationtitle))
                .setContentText(getString(R.string.notificationtext))
                .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
                .setContentIntent(pIntent)
                .setAutoCancel(true);

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


    // Custom Notification

    public void CustomNotification() {
        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.customnotification);

        String strtitle = getString(R.string.customnotificationtitle);
        String strtext = getString(R.string.customnotificationtext);

        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logosmall)
                .setTicker(getString(R.string.customnotificationticker))
                .setAutoCancel(true)
                .setContentIntent(pIntent)
                .setContent(remoteViews);

        remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
        remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy);

        remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
        remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext));

        Calendar cal = Calendar.getInstance();
        cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

        remoteViews.setTextViewText(R.id.time,sdf.format(cal.getTime()) );
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(1, builder.build());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
bstar55

似乎Android支持库v4具有一个支持此类事情的通知生成器:http : //developer.android.com/reference/android/support/v4/app/NotificationCompat.InboxStyle.html

NotificationCompat.InboxStyle似乎与4.1之前版本的设备的BigView等效。

这使您可以创建最多五行文本的较大通知,这可能会满足您的需求。

 Notification noti = new Notification.Builder()
 .setContentTitle("5 New mails from " + sender.toString())
 .setContentText(subject)
 .setSmallIcon(R.drawable.new_mail)
 .setLargeIcon(aBitmap)
 .setStyle(new Notification.InboxStyle()
     .addLine(str1)
     .addLine(str2)
     .setSummaryText("+3 more"))
 .setContentTitle("")
 .build();

这是一个简短的教程:http : //doandroidcoding.blogspot.com/2014/01/bigview-style-notification.html?m=1

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Android中自定义通知区域

来自分类Dev

自定义通知未显示在Android中

来自分类Dev

在Android中基于时间和日期的自定义通知

来自分类Dev

如何在Android中创建自定义通知

来自分类Dev

自定义通知未显示在Android中

来自分类Dev

如何在 Android 的通知中显示自定义文本

来自分类Dev

Android:自定义通知外观问题

来自分类Dev

Android Wear通知的自定义UI

来自分类Dev

带按钮的android的自定义通知

来自分类Dev

Android 自定义通知声音

来自分类Dev

iOS中的自定义推送通知

来自分类Dev

自定义通知

来自分类Dev

如何在Android中仅延伸中心区域的自定义按钮?

来自分类Dev

在自定义UIControl对象中定义自定义触摸区域

来自分类Dev

Android自定义形状按钮或imageview命中区域

来自分类Dev

Android-使自定义视图的透明区域不可单击

来自分类Dev

Android自定义形状按钮或imageview命中区域

来自分类Dev

在 Android 中单击时更新包含在自定义通知中的按钮的文本

来自分类Dev

如何在自定义NSView中绘制“拖放”区域

来自分类Dev

生产环境中的 CloudKit 自定义区域

来自分类Dev

DBR 报告中的 AWS 自定义区域缩写

来自分类Dev

Quickblox通知聊天室IOS中的自定义参数到Android

来自分类Dev

Android中自定义通知的确切时间

来自分类Dev

自定义通知声音在 NotificationChannel for android P 中不起作用

来自分类Dev

自定义下载通知

来自分类Dev

自定义ControlsFX通知

来自分类Dev

自定义通知中心

来自分类Dev

在Android中使用自定义类型Face创建自定义通知

来自分类Dev

将自定义图标(用于手写脚本)添加到通知区域xfce