NotificationCompat.BigPictureStyle可以显示图片

时间:2014-01-22 00:03:00

标签: java android notifications

想知道是否有人可以提供帮助,我在NotificationCompat.BigPictureStyle遇到一些麻烦 - 我不知道我是否遗漏了什么,但我的通知工作除了图片..我已经包含了很多来源它发生在最后......

public class OneShotAlarm extends BroadcastReceiver{
 @Override
    public void onReceive(Context context, Intent intent)
    {
        int z = 4;          //increase this number here to the total number of affirmations
        Random rand = new Random();
        int y = rand.nextInt(z);

        String[] s = null;
        s = new String[z];  //dim
        s[0] = "i am the best and you are the most amaxzing thing on ";
        s[1] = "I love and approve of myself.";
        s[2] = "you are the re2t";
        s[3] = "you are the res3t";

        String zztitle = "";
        String zzrestofline = "";
        String string = s[y];
        StringBuffer buffer = new StringBuffer();

    if (string.length() > 25){
        for(int i = 0; i < string.length(); i++) {
            // Set title to first 25 chars and rest of line to rest
           // if((i > 0) && (i % 100) == 0) {
            if(i == 25) {
                zztitle =  buffer.toString();
                buffer = new StringBuffer();
                }
            // Just adds the next character to the StringBuffer.
            buffer.append(string.charAt(i));
        }
        zzrestofline =  buffer.toString();
    }
        else {
        zztitle = s[y];
    }
        setNotification(context,zzrestofline,zztitle);
        WakeLocker.acquire(context);
        Toast.makeText(context,s[y], Toast.LENGTH_LONG).show();
        WakeLocker.release();
    }
private void setNotification(Context context, String zzmsg, String zztitle) {
    // http://codeversed.com/expandable-notifications-android/#Custom_View

    Bitmap remote_picture = null;
    remote_picture = BitmapFactory.decodeResource(context.getResources(), R.drawable.retreatgoddess);

 // Create the style object with BigPictureStyle subclass.
    NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
    notiStyle.setBigContentTitle("Big Picture Expanded");
    notiStyle.setSummaryText("Nice big picture.");

 // Add the big picture to the style.
    notiStyle.bigPicture(remote_picture);

 // Creates an explicit intent for an ResultActivity to receive.
    Intent resultIntent = new Intent(context, MainActivity.class); // Creates an explicit intent for an Activity in your app

 // This ensures that the back button follows the recommended
 // convention for the back key.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

 // Adds the back stack for the Intent (but not the Intent itself).
    stackBuilder.addParentStack(MainActivity.class);

 // Adds the Intent that starts the Activity to the top of the stack.
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(zztitle)
            .setContentText(zzmsg)
            .setTicker(zztitle)
            .setLargeIcon(remote_picture)
            .setStyle(notiStyle);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 12345; // arbitrary number
    mNotificationManager.notify(mId, mBuilder.build()); // mId allows you to update the notification later on.  

1 个答案:

答案 0 :(得分:0)

您应该知道BigPictureStyle仅适用于Android JellyBean(4.1.2)及更高版本。 NotificationCompat类负责管理与各种Android版本的兼容性。 在哪个设备版本上测试此代码?