Android Notification and icon issue

notification icon must be 24x24 dp for optimal solution on most devices.html

seejava

http://developer.android.com/design/style/iconography.html#notificationandroid

or create the following notification sizes:佈局

  • 72x72px size in drawable-xxhdpicode

  • 48x48px in drawable-xhdpihtm

  • 36x36px in drawable-hdpiget

  • 24x24px in drawable-mdpistring

  • 18x18px in drawable-ldpiit

以上尺寸是給狀態欄和提示滾動信息用圖標尺寸。 下拉之後的大圖標的尺寸就比較隨意了。io

下面是代碼實現,用的是系統默認的佈局。 舊寫法:

        
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.title);
        Intent notificationIntent = new Intent(context, LaunchActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        //Set the details of Notification 
        notification.setLatestEventInfo(context, title, message, intent);
        notification.tickerText = message;
        notification.defaults = Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.icon=R.drawable.ticker_icon;
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        InputStream is = context.getResources().openRawResource(R.drawable.large_icon);
        Bitmap bitmap =  BitmapFactory.decodeStream(is, null, opt);
        notification.largeIcon= bitmap; 
        
        //The custom of notification
        /*RemoteViews contentView = new RemoteViews(context.getPackageName(),
                R.layout.notification);
        contentView
        .setImageViewResource(R.id.notification_image, R.drawable.large_icon);
        contentView.setTextViewText(R.id.notification_title, title);
        contentView.setTextViewText(R.id.notification_text, message);
        notification.contentView = contentView;*/
        
        //show Notification
        notificationManager.notify(id, notification);
相關文章
相關標籤/搜索