1

안녕하세요, 안드로이드에 대한 새로운 메신저입니다. 내 교회를위한 간단한 온라인 라디오 응용 프로그램을 만들고 있습니다. 포블렘은 전화가 왔을 때 재생 서비스를 중지하고 다시 시작해야한다는 것입니다. 그것을 종료 지금까지 내가 잘 서비스를 중지 할 수 있지만 다시 시작됩니다 때 너무 충돌 내가 잘못전화가 안드로이드에 도착했을 때 오디오 재생을 멈추는 방법

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.IBinder; 
import android.os.PowerManager.WakeLock; 
import android.support.v4.app.NotificationCompat; 
import com.spoledge.aacdecoder.MultiPlayer; 


public class RadioService extends Service{ 


    WakeLock wakelock; 
    //TelephonyManager teleManager= null; 
    //PhoneStateListener listener; 
    NotificationManager mNotificationManager; 
    public static MultiPlayer aacPlayer; 
    public static String radio; 
    private static Boolean isTuning; 

    // private TeleListener myListener = null; 



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


    @Override 
    public void onCreate(){ 
      super.onCreate(); 
      setTuning(false); 

      setRadio(getString(R.string.rhema)); 
      aacPlayer = new MultiPlayer(); 





    } 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId){ 

     setTuning(true); 

       setTuning(true); 

      aacPlayer.playAsync(radio); 
      //CallReceiver.setTuning(true); 

      Resources r= getResources(); 
      String[] Notify = r.getStringArray(R.array.PlayNotify); 
      // prepare intent which is triggered if the 
      // notification is selected 

      Intent inten = new Intent(this, PlayerActivity.class); 
      PendingIntent pIntent = PendingIntent.getActivity(this, 0, inten, 0); 

      // build notification 
      // the addAction re-use the same intent to keep the example short 
      NotificationCompat.Builder n = new NotificationCompat.Builder(this) 
        .setContentTitle(Notify[0]) 
        .setContentText(Notify[1]) 
        .setSmallIcon(R.drawable.noti_icon) 
        .setContentIntent(pIntent) 
        .setAutoCancel(true) 
        .setOngoing(true) 
        .setContentIntent(pIntent); 

      Notification hola = n.build(); 
      startForeground(100, hola); 

      //startForeground(startId, notification); 
     return START_STICKY; 
    } 

    @Override 
     public void onDestroy() { 
      // TODO Auto-generated method stub 
      pause(); 


      //CallReceiver.setTuning(false); 
      super.onDestroy(); 

     } 
    public static boolean getTuning() { 
     return isTuning; 
    } 
    public void setTuning(boolean Tuning) { 
     isTuning = Tuning; 
    } 
    public void setRadio(String r){ 
     radio = r; 
    } 

    public void PlayNotify(){ 
     Resources r= getResources(); 
     String[] Notify = r.getStringArray(R.array.PlayNotify); 
     // prepare intent which is triggered if the 
     // notification is selected 

     Intent intent = new Intent(this, RadioService.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

     // build notification 
     // the addAction re-use the same intent to keep the example short 
     NotificationCompat.Builder n = new NotificationCompat.Builder(this) 
       .setContentTitle(Notify[0]) 
       .setContentText(Notify[1]) 
       .setSmallIcon(R.drawable.noti_icon) 
       .setContentIntent(pIntent) 
       .setAutoCancel(true); 

     n.setOngoing(true); 
     n.setContentIntent(pIntent); 
     //NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      // mId allows you to update the notification later on. 
      mNotificationManager.notify(100, n.build()); 
    } 

    public static void play(){ 
     aacPlayer.playAsync(radio); 

    } 
    public static void pause(){ 
     aacPlayer.stop(); 

    } 

} 

을하고 어떤 메신저 잘 모릅니다 이것은 방송

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 

public class CallReceiver extends BroadcastReceiver{ 
    TelephonyManager telManager; 
    Context context; 
    public static boolean Tuning = false; 
    PlayerActivity mainActivity; 
    NotificationManager mNotificationManager; 

@Override 
public void onReceive(Context context, Intent intent) { 


      mainActivity=new PlayerActivity(); 


    this.context=context; 

    telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

} 

private final PhoneStateListener phoneListener = new PhoneStateListener() { 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     try { 
      switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: { 
       //PAUSE 
       if(Tuning){ 

        context.stopService(new Intent(context,RadioService.class)); 
        } 

      break; 
      } 
      case TelephonyManager.CALL_STATE_OFFHOOK: { 
       if(Tuning){ 

        context.stopService(new Intent(context,RadioService.class)); 
       } 

      break; 
      } 
      case TelephonyManager.CALL_STATE_IDLE: { 
       //PLAY 
       if(Tuning){ 

        showNotification(context); 
        context.stopService(new Intent(context,RadioService.class));} 
      break; 
      } 
      default: { } 
      } 
      } catch (Exception ex) { 

      } 
     } 

    }; 

    public static void setTuning(boolean r){ 

     Tuning = r; 
    } 
    private void showNotification(Context context) { 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(context, PlayerActivity.class), 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.noti_icon) 
       .setContentTitle("Rhema Stereo") 
       .setContentText("Vuelve a Sintonizarnos."); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1, mBuilder.build());} 

} 

위어입니다 그게 내가 시작할 수있는 활동에서 나온거야. 서비스 잘 정상 그러나 여기에서 나는 그것을 단지 멈출 수있다. 지금은 서비스

+0

전화 서비스 시작을위한 코드를 추가해야한다고 생각합니다 .CALL_STATE_IDLE 케이스 – MFP

답변

1

내가이 문제에 대해 다른 방법을 제안합니다. 전화가 문제가 더 복잡하다, 그러나, 전화

을 얻을 때

내가 중지하고 다시 시작 재생 서비스가 필요합니다 : 당신은 것을 주장. 또한 예를 들어 알림이 도착하거나 사용자가 다른 음악 플레이어를 시작한 경우와 같이 재생을 중지해야합니다. 이러한 모든 위치를 독립적으로 모니터링하는 대신 Android는 오디오 포커스이라고하는이 개념을 기본적으로 지원합니다. 넓은 의미에서, 한 번에 하나의 앱에서만 오디오를 집중시킬 수 있으며 요청하면 포기해야한다는 것을 의미합니다.

이 동작은 OnAudioFocusChangeListener을 사용하여 얻을 수 있습니다.

기본적으로 수행해야합니다 재생을 시작하기 전에

  • 요청 오디오 초점을 맞 춥니 다.
  • 효과적으로 얻은 경우에만 재생을 시작하십시오.
  • 재생을 중지하면 초점을 포기하십시오.
  • 볼륨을 낮추거나 재생을 모두 중지하여 오디오 포커스 손실을 처리합니다.

안드로이드 설명서의 Managing Audio Focus 문서를 확인하십시오.

+0

고마워요! 그게 내가 뭘 완벽하게 작동이 필요했습니다 그냥 – user3739754

+0

@ user3739754 난 기뻐요! 그것이 유용하다고 생각한다면 대답을 수락하는 것을 잊지 마십시오 :) – matiash

0

를 다시 활동을 얻기 위해 알림을 사용하여 메신저 다시 시작 그래서 당신은

IntentFilter receiverFilter = new IntentFilter(
     Intent.ACTION_HEADSET_PLUG); 
BootBroadcast receiver = new BootBroadcast(); 
registerReceiver(receiver, receiverFilter); 

는 다음 같은 필요가 수신기 manifest.xml에 등록하려면 현재 활동이 조각을 추가 할 필요가

예 :

<receiver 
      android:name="YOUR_ACTION_STRING.BootCompletedReceiver" 
      android:enabled="true" 
      android:exported="false" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

는 또한 BootBroadcast 클래스 내에서이 플래그를 추가

newIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
관련 문제