2014-01-22 36 views
2

想知道如果有人可以幫助,我有一些trouble- 我不知道我是否缺少的東西,但我的通知工程除了圖片..我已經包括了很多源代碼大部分發生在最後..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. 
+0

你的代碼看起來不錯,所以看起來問題是加載位圖。嘗試使用相同的代碼將此圖像加載到常規ImageView中:BitmapFactory.decodeResource(context.getResources(),R.drawable.retreatgoddess);那樣有用嗎? – EZDsIt

回答

1

您應該知道BigPictureStyle僅適用於Android JellyBean(4.1.2)及更高版本。 NotificationCompat類負責管理與各種Android版本的兼容性。 您在測試此代碼的哪個設備版本?