2014-10-05 5 views
5

मेरे पास निम्न सेवा है जो मेरे आवेदन में अधिसूचना दिखाती है, यह अभी तक ठीक दिखती है, लेकिन यह क्लिक घटनाओं को कुछ कैसे संभालती नहीं है। मैं इन्हें कैसे संभाल सकता हूं?बटन के साथ अधिसूचना घटनाओं को संभाल नहीं देती

आवश्यकता: मैं इस अधिसूचना को तब तक प्रस्तुत करना चाहता हूं जब तक वीडियो चल रहा हो। मुझे उम्मीद है कि यह करने का यह तरीका है।

public class CustomNotifications extends Service implements 
     INotificationService { 

    private static CustomNotifications instance; 

    private Context ctx; 
    private NotificationManager mNotificationManager; 

    private String start = "play"; 
    private String stop = "stop"; 

    private VideoCastManager mCastManager; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if (null != intent) { 
      String action = intent.getAction(); 
      if (action != null) { 
       if (action.equals(start)) { 

       } else if (action.equals(stop)) { 

       } 
      } 
     } 
     return 1; 
    } 

    public void startNofication(Activity activity, String text) { 
     ctx = this.getApplicationContext(); 
     Intent resultIntent = new Intent(this, FullscreenActivity.class); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 
       0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Media Cast").setTicker(text) 
       .setAutoCancel(false).setOngoing(true) 
       .setContentIntent(resultPendingIntent); 

     RemoteViews contentView = new RemoteViews(ctx.getPackageName(), 
       com.me.app.mediacast.R.layout.notification_layout); 

     contentView.setTextViewText(
       com.me.app.mediacast.R.id.notifcation_label, text); 
     contentView.setImageViewResource(
       com.me.app.mediacast.R.id.notification_button, 
       R.drawable.ic_av_play_dark); 
     // set the button listeners 
     setListeners(contentView); 

     mBuilder.setContent(contentView); 

     int mNotificationId = 001; 
     // NotificationManager mNotifyMgr = (NotificationManager) activity 
     // .getSystemService(activity.NOTIFICATION_SERVICE); 
     // mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
     startForeground(mNotificationId, mBuilder.build()); 
    } 

    private void setListeners(RemoteViews contentView) { 
     Intent radio = new Intent(start); 
     radio.setPackage(getPackageName()); 
     PendingIntent pRadio = PendingIntent.getActivity(this, 0, radio, 0); 
     contentView.setOnClickPendingIntent(
       com.me.app.mediacast.R.id.notification_button, pRadio); 
    } 

    @Override 
    public boolean stopService() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean startService(String text) { 
     startNofication((Activity) ContentHelper.getCurrentActivity(), text); 
     return true; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     try { 
      mCastManager = VideoCastManager.getInstance(); 
     } catch (CastException e) { 
      e.printStackTrace(); 
     } 
     mCastManager.setNottificationService(this); 
     mCastManager.startNotificationService(); 
    } 
} 

निम्नलिखित लेआउट xml है।

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black" > 

    <TextView 
     android:id="@+id/notifcation_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/btn1" /> 

    <TextView 
     android:id="@+id/notifcation_subtext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/notifcation_label" /> 

    <ImageButton 
     android:id="@+id/notification_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_margin="10dp" 
     android:layout_marginRight="19dp" /> 

</RelativeLayout> 

मैं सिर्फ यह क्लिक प्राप्त करने के लिए भी हैंडल हां, तो मैं बटन को थामने के लिए या टॉगल खेलने आवेदन की स्थिति पर निर्भर करता है दिखा सकते हैं कोशिश कर रहा हूँ।

इसे देखने के लिए अग्रिम धन्यवाद।

+2

आप http://stackoverflow.com/questions/21872022/notification-for-android-music-player और http://stackoverflow.com/questions पर जवाब करने की कोशिश की है/24436977/प्ले-पॉज़-बटन-छवि-इन-अधिसूचना-एंड्रॉइड? –

उत्तर

1

मैंने इसे 'सेवा' में निम्नलिखित का उपयोग करके हल किया है। मैनिफेस्ट फ़ाइल में इरादा फ़िल्टर जोड़ने के लिए भी मत भूलना।

Intent resultIntent = new Intent(this, FullscreenActivity.class); 
    resultIntent.setAction(Intent.ACTION_MAIN); 
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    PendingIntent pintent = PendingIntent.getActivity(ctx, 0, resultIntent, 
      0); 
    PendingIntent pendingIntent = null; 
    Intent intent = null; 
    // Inflate a remote view with a layout which you want to display in the 
    // notification bar. 
    if (mRemoteViews == null) { 
     mRemoteViews = new RemoteViews(getPackageName(), 
       R.layout.custom_notification); 
    } 

    /** 
    * add handlers to the buttons 
    * 
    */ 
    Uri uri = info.getMetadata().getImages().get(0).getUrl(); 
    Bitmap bmp = null; 
    if (uri != null) { 
     URL url; 
     try { 
      url = new URL(uri.toString()); 
      bmp = BitmapFactory.decodeStream(url.openStream()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
    if (bmp != null) { 
     mRemoteViews.setImageViewBitmap(R.id.iconView, bmp); 
    } 
    if (isPlaying) { 
     mRemoteViews.setImageViewResource(R.id.playPauseView, 
       R.drawable.ic_av_pause_sm_dark); 
    } else { 
     mRemoteViews.setImageViewResource(R.id.playPauseView, 
       R.drawable.ic_av_play_sm_dark); 
    } 
    mRemoteViews.setTextViewText(R.id.titleView, info.getMetadata() 
      .getString(MediaMetadata.KEY_TITLE)); 
    intent = new Intent(play); 
    pendingIntent = PendingIntent.getService(getApplicationContext(), 
      REQUEST_CODE_STOP, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    mRemoteViews.setOnClickPendingIntent(R.id.playPauseView, pendingIntent); 

    Intent destroy = new Intent(stop); 
    PendingIntent pendingIntent1 = PendingIntent.getService(
      getApplicationContext(), REQUEST_CODE_STOP, destroy, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    mRemoteViews.setOnClickPendingIntent(R.id.removeView, pendingIntent1); 

    /** 
    * 
    * 
    */ 
    // Create the notification instance. 
    mNotification = new NotificationCompat.Builder(getApplicationContext()) 
      .setSmallIcon(R.drawable.ic_launcher).setOngoing(true) 
      .setWhen(System.currentTimeMillis()).setContent(mRemoteViews) 
      .setContentIntent(pintent).build(); 

    mNotifiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    mNotifiManager.notify(NOTIFICATION_ID, mNotification); 

AndroidManifest

<intent-filter> 
     <action android:name="com.me.app.mediacast.PLAY" /> 
     <action android:name="com.me.app.mediacast.PAUSE" /> 
     <action android:name="com.me.app.mediacast.STOP" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
0

RemoteViews में Notification में विजेट पर क्लिक ईवेंट के लिए कोई आधिकारिक समर्थन नहीं है, और वे अधिकांश उपकरणों पर काम नहीं करेंगे। कृपया notification actions का उपयोग करें।

मैं यह अधिसूचना तब तक उपस्थित होना चाहूंगा जब तक वीडियो चल रहा हो। मुझे उम्मीद है कि यह करने का यह तरीका है।

वीडियो प्लेबैक को नियंत्रित करने के लिए Notification का उपयोग करना असामान्य होगा। जब वीडियो टैप किया जाता है, तो अन्य वीडियो प्लेयर नियंत्रण कक्ष लाते हैं, या अन्यथा खिलाड़ी पर टच इवेंट या इशारे का उपयोग करते हैं।

+0

उत्तर के लिए धन्यवाद। हाँ, वीडियो के लिए अधिसूचना असामान्य है। मैं यह उल्लेख करना भूल गया कि मेरा ऐप क्रोमकास्ट डोंगल में वीडियो स्ट्रीम करता है। – Zeus

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