2014-08-27 57 views
-4

如何阻止Android自動殺死應用程序?我的電臺應用程序在5:15分鐘後自動殺死。我需要我的廣播播放機工作,而不是被殺死。 我使用startForegroundCompat但不工作好,還是自動殺死15後:20分鐘如何阻止Android自動殺死應用程序?

package com.test.test; 

import java.io.IOException; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.AudioManager; 
import android.media.AudioManager.OnAudioFocusChangeListener; 
import android.media.MediaPlayer; 
import android.os.IBinder; 
import android.preference.PreferenceManager; 
import android.util.Log; 

public class StreamService extends Service { 
    private static final String TAG = "StreamService"; 
    MediaPlayer mp; 
    boolean isPlaying; 
    Intent MainActivity; 

    SharedPreferences prefs; 
    SharedPreferences.Editor editor; 
    Notification n; 
    NotificationManager notificationManager; 

    // Change this int to some number specifically for this app 
    int notifId = 85; 

    private OnAudioFocusChangeListener focusChangeListener = 
       new OnAudioFocusChangeListener() { 
         public void onAudioFocusChange(int focusChange) { 
         switch (focusChange) { 

           case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) : 
           // Lower the volume while ducking. 
           mp.setVolume(0.2f, 0.2f); 
           break; 
           case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) : 
           mp.pause(); 
           break; 

           case (AudioManager.AUDIOFOCUS_LOSS) : 
           mp.stop(); 

           break; 

           case (AudioManager.AUDIOFOCUS_GAIN) : 
           // Return the volume to normal and resume if paused. 
           mp.setVolume(1f, 1f); 
           mp.start(); 
           break; 
           default: break; 
    } 
    } 
    }; 

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

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d(TAG, "onCreate"); 

     // Init the SharedPreferences and Editor 
     prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     editor = prefs.edit(); 

     // Set up the buffering notification 
     notificationManager = (NotificationManager) getApplicationContext() 
       .getSystemService(NOTIFICATION_SERVICE); 
     Context context = getApplicationContext(); 

     String notifTitle = context.getResources().getString(R.string.app_name); 
     String notifMessage = context.getResources().getString(R.string.buffering); 

     n = new Notification(); 
     n.icon = R.drawable.ic_launcher; 
     n.tickerText = "Buffering"; 
     n.when = System.currentTimeMillis(); 

     Intent nIntent = new Intent(context, MainActivity.class); 
     nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

     n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 

     notificationManager.notify(notifId, n); 

     // It's very important that you put the IP/URL of your ShoutCast stream here 

     String url = "http://90.121.123.129:8272"; 
     mp = new MediaPlayer(); 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 

     try { 
      mp.setDataSource(url); 
      mp.prepare(); 
      mp.prepareAsync(); 
      mp.setOnErrorListener(null); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "SecurityException"); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "IllegalStateException"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "IOException"); 
     } 
    } 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onStart(Intent intent, int startId) { 
     Log.d(TAG, "onStart"); 
     mp.start(); 

     // Set the isPlaying preference to true 
     editor.putBoolean("isPlaying", true); 
     editor.commit(); 

     Context context = getApplicationContext(); 
     String notifTitle = context.getResources().getString(R.string.app_name); 
     String notifMessage = context.getResources().getString(R.string.now_playing); 

     n.icon = R.drawable.ic_launcher; 
     n.tickerText = notifMessage; 
     n.flags = Notification.FLAG_NO_CLEAR; 
     n.when = System.currentTimeMillis(); 

     Intent nIntent = new Intent(context, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

     n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 
     // Change 5315 to some nother number 
     notificationManager.notify(notifId, n); 
    /* 
     AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

     // Request audio focus for playback 
     int result = am.requestAudioFocus(focusChangeListener, 
     // Use the music stream. 
     AudioManager.STREAM_MUSIC, 
     // Request permanent focus. 
     AudioManager.AUDIOFOCUS_GAIN); 


     if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
     // other app had stopped playing song now , so u can do u stuff now . 

     } 
     */ 

      } 

    @SuppressWarnings("deprecation") 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // We want this service to continue running until it is explicitly 
     // stopped, so return sticky. 
     mp.start(); 
     // Set the isPlaying preference to true 
       editor.putBoolean("isPlaying", true); 
       editor.commit(); 

       Context context = getApplicationContext(); 
       String notifTitle = context.getResources().getString(R.string.app_name); 
       String notifMessage = context.getResources().getString(R.string.now_playing); 

       n.icon = R.drawable.ic_launcher; 
       n.tickerText = notifMessage; 
       n.flags = Notification.FLAG_NO_CLEAR; 
       n.when = System.currentTimeMillis(); 

       Intent nIntent = new Intent(context, MainActivity.class); 
       PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

       n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 
       // Change 5315 to some nother number 
       notificationManager.notify(notifId, n); 
       AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

       // Request audio focus for playback 
       int result = am.requestAudioFocus(focusChangeListener, 
       // Use the music stream. 
       AudioManager.STREAM_MUSIC, 
       // Request permanent focus. 
       AudioManager.AUDIOFOCUS_GAIN); 


       if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
       // other app had stopped playing song now , so u can do u stuff now . 

       } 



     return START_STICKY; 
    } 
    void startForegroundCompat(int id, Notification notification) { 
     // If we have the new startForeground API, then use it. 

      mp.start(); 

      return; 

     } 
    @Override 
    public void onDestroy() { 
     Log.d(TAG, "onDestroy"); 
     mp.stop(); 
     mp.release(); 
     mp = null; 
     editor.putBoolean("isPlaying", false); 
     editor.commit(); 
     notificationManager.cancel(notifId); 
     AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

     am.abandonAudioFocus(focusChangeListener); 


    } 

} 
+1

您應該檢查您的應用是否未濫用Android生態系統。它可能消耗太多內存。檢查內存泄漏。使用分析工具來查找原因。 – 2014-08-27 09:54:05

+0

你的意思是'onStartCommand ... return START_STICKY;'? – 2014-08-27 09:57:25

+0

我是一名新的android開發者:) – 2014-08-27 09:57:51

回答

6

你不能做到這一點,你永遠不應該這樣做。 Android決定應該運行哪個應用程序,以及應該殺死哪些應用程序。您必須設計您的應用程序來處理此類情況並保存敏感數據。

+0

你的意思是'onStartCommand ... return START_STICKY;'? – 2014-08-27 09:53:24

+0

請考慮使用服務進行後臺任務http://developer.android.com/training/run-background-service/index.html。另外,看看保存數據選項http://developer.android.com/training/basics/data-storage/index.html – 2014-08-27 10:01:04

+0

謝謝你的幫助 – 2014-08-27 10:05:44

相關問題