如何阻止Android自动查杀应用?
我的收音机应用程序在5:15分钟后自动杀死。我需要我的无线电播放器一直工作而不是被杀死。
我使用startForegroundCompat
但效果不佳,并且在15:20分钟后仍被自动杀死
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);
}
}
答案 0 :(得分:6)
你不能这样做,你永远不应该这样做。是Android决定应该运行什么应用程序,以及应该杀死什么。您必须设计应用程序以处理此类情况并保存敏感数据