0

Ich möchte Aktion in Benachrichtigung hinzufügen, die beim Senden Broadcast an den gleichen Dienst sendet, in dem ein Lied spielt, und dort in Boradcast Empfänger-Klasse, ich möchte den spielenden Song stoppen. Aber ich kann nicht bekommen, dass die Sendung vom Empfänger empfangen wird. Eine detaillierte Lösung würde sehr geschätzt werden.Broadcast in ausstehende Absicht funktioniert nicht über Benachrichtigung

package com.example.ranasarmadrajput.servicestask; 

import android.content.Intent; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent i = getIntent(); 
     if(i.getAction().equals("playpause")){ 
      Toast.makeText(this,"sss",Toast.LENGTH_LONG).show(); 
     } 
    } 
    public void selectsong(View view){ 
     Intent intent= new Intent(android.content.Intent.ACTION_GET_CONTENT); 
     String type = "audio/*"; 
     intent.setType(type); 
     startActivityForResult(intent,2); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 2) { 
      if (data != null) { 
       Intent intent = new Intent(this,musicplayer.class); 
       intent.setAction("Play"); 
       intent.setData(data.getData()); 
       startService(intent); 

      } 
     } 
    }} 

und Service-Klasse

package com.example.ranasarmadrajput.servicestask; 

import android.app.IntentService; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Intent; 
import android.content.Context; 
import android.content.IntentFilter; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.session.MediaSession; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.app.NotificationCompatBase; 
import android.widget.Toast; 

/** 
* An {@link IntentService} subclass for handling asynchronous task requests in 
* a service on a separate handler thread. 
* <p> 
* TODO: Customize class - update intent actions, extra parameters and static 
* helper methods. 
*/ 
public class musicplayer extends IntentService { 
    MediaPlayer mp; 

    public musicplayer() { 
     super("musicplayer"); 
    } 


    @Override 
    protected void onHandleIntent(Intent intent) { 
     if (intent != null) { 
      final String action = intent.getAction(); 
      if (action.equals("Play")) { 
       try { 
        Uri u= intent.getData(); 
        mp = new MediaPlayer(); 
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        if (u != null) { 
         mp.setDataSource(getApplicationContext(), u); 
         mp.prepare(); 
         mp.start(); 
         mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
          @Override 
          public void onCompletion(MediaPlayer mp) { 
           mp.release(); 
          } 
         }); 
        } else { 
         Toast.makeText(this, "Data is null...", Toast.LENGTH_SHORT).show(); 
        } 
        Intent in=new Intent(this,ReceiverClassName.class); 
        in.setAction("playpause"); 
        PendingIntent pi=PendingIntent.getBroadcast(this,0,in,0); 
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
          .setContentTitle("title") 
          .setContentText("text") 
          .setAutoCancel(true) 
          .setSmallIcon(R.mipmap.ic_launcher) 
          .addAction(R.mipmap.ic_launcher,"sss",pi); 

        Notification notification = builder.build(); 
        NotificationManager manager = (NotificationManager) 
          getSystemService(Context.NOTIFICATION_SERVICE); 
        manager.notify(22, notification); 
       } catch (Exception exception) { 
        exception.printStackTrace(); 
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); 
       } 
       /*IntentFilter filter = new IntentFilter(); 
       filter.addAction("playpause"); 
       registerReceiver(new ReceiverClassName(), filter);*/ 

      } 
     } 
    } 

    public class ReceiverClassName extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String Action = intent.getAction(); 
      if(Action.equals("playpause")){ 
       mp.stop(); 

      } 
     } 
    } 
} 

Antwort

0

folgen Sie diesem Thread auf Google Code https://code.google.com/p/android/issues/detail?id=61850

es zu beheben, müssen Sie PendingIntent.FLAG_CANCEL_CURRENT Flagge zu Ihrem PendingIntent hinzuzufügen.

PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0, 
      stopIntent2, PendingIntent.FLAG_CANCEL_CURRENT); 
Verwandte Themen