2013-08-21 7 views
26

मैंने अपने सिस्टम संगीत ऐप को डिमंपिल्ड किया (एंड्रॉइड जीबी 2.3.7 के लिए सोनी एरिक्सन से) क्योंकि मैं अधिसूचना लेआउट बदलना चाहता हूं। मुझे वह विधि मिली जो इस कोड के साथ अधिसूचना बनाता है:अधिसूचना लेआउट बदलें

private void sendStatusBarNotification(Track paramTrack) 
{ 
    if (paramTrack != null) 
    { 
     NotificationManager localNotificationManager = (NotificationManager)this.mContext.getSystemService("notification"); 
     String str = paramTrack.getArtist(); 

     if ((str == null) || (str.equals(this.mContext.getString(2131361954)))) 
      str = this.mContext.getString(2131361798); 

     Notification localNotification = new Notification(2130837696, paramTrack.getTitle() + " - " + str, System.currentTimeMillis()); 
     localNotification.flags = (0x2 | localNotification.flags); 
     localNotification.flags = (0x20 | localNotification.flags); 

     PendingIntent localPendingIntent = PendingIntent.getActivity(this.mContext, 0, new Intent(this.mContext, MusicActivity.class), 268435456); 
     localNotification.setLatestEventInfo(this.mContext, paramTrack.getTitle(), str, localPendingIntent); 
     localNotificationManager.notify(0, localNotification); 
    } 
} 

मेरा प्रश्न अब है: मैं अधिसूचना लेआउट कैसे बदल सकता हूं? मैं एक लेआउट बनाना चाहता हूं जो मूल एंड्रॉइड अधिसूचना लेआउट की तरह दिखता है लेकिन अधिसूचना के दाईं ओर एक अतिरिक्त छवि के साथ। मैं यह कैसे कर सकता हूँ?

उत्तर

76

पहले अपनी सूचना के लिए एक एक्सएमएल बनाएं।

custom_notification.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" > 
    <ImageView android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     style="Custom Notification Title" /> 
    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:layout_below="@id/title" 
     style="Custom Notification Text" /> 
</RelativeLayout> 

अब जावा कोड:

public class MainActivity extends Activity { 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     Notification notification = new Notification(icon, "Custom Notification", when); 

     NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
     contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
     contentView.setTextViewText(R.id.title, "Custom notification"); 
     contentView.setTextViewText(R.id.text, "This is a custom layout"); 
     notification.contentView = contentView; 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.contentIntent = contentIntent; 

     notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification 
     notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
     notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration 
     notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

     mNotificationManager.notify(1, notification); 
    } 

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

आशा इस बात आप के लिए काम करता है।

संपादित करें: यदि आप this जैसी समस्या में आते हैं तो आप भी देख सकते हैं।

इसके अलावा, आप अधिक जानकारी के लिए here पर जा सकते हैं।

संपादित अप्रैल 26 वर्ष 2016 आप नीचे के रूप में Notification उदाहरण बनाने के लिए NotificationCompat.Builder उपयोग कर सकते हैं:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(icon) 
      .setContent(contentView) 
      .setContentTitle("Custom Notification") 
      .setWhen(when); 
... 
mNotificationManager.notify(1, notificationBuilder.build()); 
+0

कि के लिए धन्यवाद। आपको बक्षीस मिलेगा लेकिन मैं इसे आपको एक घंटे में दे सकता हूं;) – Cilenco

+2

@Cilenco कोई समस्या नहीं प्रिय। मैंने आपको बक्षीस के लिए बिल्कुल मदद नहीं की। मुझे बस खुशी है कि मेरा समाधान आपके लिए काम करता है, यह है ... :) मुझे जो कुछ भी पता है, उससे लोगों की मदद करना अच्छा लगता है। हैप्पी कोडिंग ...! –

+0

@ shree202 मुझे एक सुझाव देता है। मैं इस लिंक की तरह प्ले/पॉज़ को नियंत्रित करने के लिए रिमोट व्यू का उपयोग करके एक अधिसूचना बड़ा दृश्य बनाता हूं (स्टैक ओवरफ्लो।कॉम/प्रश्न/1450836 9/...) सभी सही हैं, लेकिन जब मैं डिवाइस से बैक बटन पर क्लिक करता हूं और एप्लिकेशन से बाहर क्लिक करता हूं तो ईवेंट (प्ले/पॉज़/फॉरवर्ड/क्लोज़) बटन काम नहीं करता है। कृपया मेरी मदद करें। –

0

यह अच्छा होगा अगर हम लेआउट डेटा प्राप्त कर सकें, चाहे वह कोड या एक्सएमएल में निर्धारित हो।

जो प्रदान किया गया है, उससे दूर जा रहा है, मैं कह सकता हूं कि आपको छवि प्राप्त करनी है और इसे new Notification(2130837696, paramTrack.getTitle() + " - " + str, System.currentTimeMillis()); घोषणा में रखना है।

ईमानदारी से यह है कि मैं आपको जो कुछ भी प्रदान करता हूं, वह सब कुछ दे सकता हूं। शुभकामनाएँ!

2

कस्टम अधिसूचना लेआउट बनाने के लिए एंड्रॉइड एपीआई गाइड on the subject देखें। ऐसा लगता है कि आप RemoteViews कक्षा का उपयोग करने जा रहे हैं।

4

यहाँ मैं एक स्क्रीनशॉट, पहले स्क्रीन सामग्री पोस्ट की एक शीर्षक संलग्न किया है और जब हम ऐप नाम के दाईं ओर नीचे तीर पर क्लिक करते हैं तो यह दूसरे स्क्रीनशॉट की ओर जाता है जो पुश अधिसूचना के लिए कस्टम लेआउट है। निम्नलिखित नमूना लेआउट है जिसे मैंने अपने लिए डिज़ाइन किया है। कस्टम लेआउट से अधिसूचना बनाने के लिए

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="4dp" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
card_view:cardCornerRadius="5dp" 
card_view:cardUseCompatPadding="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:background="#80000000" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:src="@mipmap/ic_launcher" 
     android:layout_width="50dp" 
     android:layout_height="match_parent" 
     android:padding="10dp" 
     android:layout_marginLeft="5dp" 
     android:background="@null" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:scaleType="centerCrop"/> 
    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:minHeight="48dp" 
     android:paddingBottom="16dp" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:paddingTop="16dp" 
     android:background="@android:color/transparent" 
     android:textColor="@android:color/white" 
     tools:text="Test"/> 
</LinearLayout> 
<ImageView 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:adjustViewBounds="true" 
    android:contentDescription="@null" 
    android:scaleType="centerCrop" 
    android:src="@drawable/placeholder"/> 
</LinearLayout> 

विधि,

public static void createNotification(String title, String body,String image_url, Context context, int notificationsId, String single_id) { 
    Intent notificationIntent; 

    long when = System.currentTimeMillis(); 
    int id = (int) System.currentTimeMillis(); 

    Bitmap bitmap = getBitmapFromURL(image_url); 
    NotificationCompat.BigPictureStyle notifystyle = new NotificationCompat.BigPictureStyle(); 
    notifystyle.bigPicture(bitmap); 
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); 
    contentView.setImageViewBitmap(R.id.image, bitmap); 
    contentView.setTextViewText(R.id.title, body); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setStyle(notifystyle) 
      .setCustomBigContentView(contentView) 
      .setContentText(body); 
    NotificationManager mNotificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationIntent = new Intent(context, SinglePost.class); 
    notificationIntent.putExtra("single_id",single_id); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0); 

    Notification notification = mBuilder.build(); 
    notification.contentIntent = contentIntent; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 


    mNotificationManager.notify(notificationsId, notification); 

} 

public static Bitmap getBitmapFromURL(String strURL) { 
    try { 
     URL url = new URL(strURL); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
+0

बहुत बढ़िया महोदया, बहुत मदद की। –

संबंधित मुद्दे