IntentService即使应用程序被杀,我也想继续在后台运行.并且被"杀死"我的意思是按住主页按钮很长时间 - > 查看所有正在运行的应用程序 - >将我的应用程序放在一边 - > 应用程序被杀或按下后退按钮很长时间 - > 应用程序被杀
我的代码如下.在我的MainActivity中:
Intent intent = new Intent(this, MyService.class);
this.startService(intent);
Run Code Online (Sandbox Code Playgroud)
在我的MyService中:
public class MyService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("MyService started");
run();
}
private void run() {
while (true){
System.out.println("MyService still running");
doSomething();
waitSomeTime();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到应用程序打开时服务正在运行.当我通过主页按钮最小化应用程序时,它仍在运行.当我通过后退按钮关闭应用程序时,它仍在运行.但如果我如上所述杀死它,它就会停止.我该如何解决这个问题?
我在启动活动时启动服务(或重新启动正在运行的服务),使用:
Intent intent = new Intent(this, MyService.class);
startService(intent);
稍后基于某些操作,相同的活动将使用绑定到服务
bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
当活动被破坏时,我打电话
unbindService(mConnection);
Run Code Online (Sandbox Code Playgroud)
早些时候,当我从应用程序托盘中杀死相同的活动/应用程序并在运行的应用程序下显示"消息1进程1服务正在运行"时,该服务用于重新启动.
现在,该服务不会在杀死相同的活动/应用程序时重新启动.
我收到消息"0进程1服务正在运行",这意味着该服务实际上没有运行.
应用程序关闭时,服务不会重新启动.我的申请包含一项活动.在系统引导后启动时,服务也会成功启动.
当我使用startService()启动它时,为什么服务进程会被杀死?
编辑
从应用程序托盘关闭应用程序后,该服务用于重新启动.但现在突然使用相同的代码,它没有.当我关闭它时,它也会与其他应用程序一起发生.例如.

在我的Android应用程序中,我通过调用从Activity中启动IntentService
startService(new Intent(this, MyService.class));
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力.我可以在Activies之间移动,按Home键切换到其他应用程序......它仍然有效.但是如果我从最近的屏幕上删除我的应用程序,我的服务就会停止.我怎么能避免这个?换句话说,即使我的应用程序已从最近的应用程序关闭,如何保持我的服务运行?
我的服务代码如下:
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Here I run a loop which takes some time to finish
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个服务,即使在用户从正在运行的进程菜单中关闭应用程序(通过将进程移出屏幕)之后,也会为我执行后台作业.
我尝试做的是通过声明它在不同的过程中创建服务:
<service
android:name=".service.Service"
android:enabled="true"
android:icon="@drawable/ic_launcher"
android:process=":my_process" >
</service>
Run Code Online (Sandbox Code Playgroud)
而onStartCommand()是:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个始终保持活动的服务,即使用户关闭了应用程序.根据这些线程
这可以通过IntentServices或Service.START_STICKY来完成
然而,我尝试了两种类型的服务而没有成功.换句话说,当用户关闭应用程序时,我的服务就会被杀死.有人可以指出这是否可以做到以及如何做?这是我没有成功的尝试:
使用IntentService:
public class MyIntentService extends IntentService {
private final int mPollingTimeMS = 500;
private int mInitializationPollingCount = 0;
private Thread mPollThread;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mPollThread = new Thread() {
public void run() {
while (true) {
try {
Log.e(Constants.Engine.LOGGER_TAG_DEV,
"SDK Service Running: " +
mInitializationPollingCount * mPollingTimeMS +
"ms have elapsed");
mInitializationPollingCount++;
sleep(mPollingTimeMS);
} catch (Exception e) {
StackTraceElement trace = new Exception().getStackTrace()[0];
Logger.e(Constants.Engine.LOGGER_TAG_APP, "[Exception:" + …Run Code Online (Sandbox Code Playgroud) 当我点击播放按钮播放歌曲并将歌曲mp3与它捆绑在一起并在onStartCommand中接收时,我开始服务.问题是当我启动服务的活动结束时,我的服务再次调用onStartCommand.当它接收该捆绑时它不存在,因为这次活动没有启动它.因此,我在准备媒体播放器时遇到了非法的例外情况.我没有约束我的服务.
为什么在我的活动结束时会调用onStartCommand?
启动服务:
Intent i = new Intent(SongList.this,MyService.class);
i.putExtra("songURL", user.songurlz);
i.putExtra("songNAME", songplay_name);
startService(i);
Run Code Online (Sandbox Code Playgroud)
服务类:
public class MyService extends Service {
static MediaPlayer mediaPlayer;
static int pauseplay;
static NotificationManager notificationManagerPlay;
static CharSequence tickerText;
static Notification notification4;//Notification variable(for song playing)
TelephonyManager telephonyManager;
PhoneStateListener listener;
static Notification notification;
static NotificationManager mNotificationManager;
String songurl;
String songname;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
pauseplay = 1;
MainMenu.serviceRunning …Run Code Online (Sandbox Code Playgroud) 我有一个服务的应用程序.服务由app在create method的主要活动中启动.我退出应用程序时出现问题:服务停止,有时立即重启,有时最近重启.我在4.1.2版和2.3.6版中测试过.版本2.3.6中不再提供服务.我的方法有误吗?在停止和重新启动时间之间,必须阻止的呼叫可以到来.退出应用程序时有没有办法停止服务?注意:我无法使用远程服务.
我添加了startForeground服务,但同样的问题.当我退出应用程序服务停止而不重启版本2.3.3甚至我看到notification.But phoneStateListener取空值我怎样才能确保当我退出应用程序时,服务不会停止
//application main metod
@Override
protected void onCreate(Bundle savedInstanceState) {
//.....
startService(new Intent(getApplicationContext(), MyPhoneStateListener.class));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
setCallListener();
setNoti();
return START_STICKY;
}
private void setCallListener()
{
try
{
if (phoneStateListener==null)
{
phoneStateListener = new StateListener();
telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
catch(Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
private void setNoti(){
Notification notification= new Notification(R.drawable.ic_launcher, getResources().getString(R.string.app_name), System.currentTimeMillis());
Intent main = new Intent(this, MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, …Run Code Online (Sandbox Code Playgroud) 我应该把我的改装调用放在Android服务类中吗?对于我的应用程序,我在某些类中调用内部改造,例如下面
Call<ArrayList<CantItem>> mycall = retrofitcalls.getCanteenItems("url.php", urldatamap);
mycall.enqueue(new Callback<ArrayList<CantItem>>() {
@Override
public void onResponse(Response<ArrayList<CantItem>> response, Retrofit retrofit) {
int code = response.code();
Log.d("code ", String.valueOf(code));
if (code == 200 || code == 201) {
ArrayList<CantItem> cantitems = response.body();
Log.d("retrieved", "returned items");
savedToSharedPrefs(createString(cantitems));
cantmap = createMap(cantitems);
presenter.updateView(cantmap);
}
}
@Override
public void onFailure(Throwable t) {
Log.d("couldnt retrieve", "failure");
}
});
Run Code Online (Sandbox Code Playgroud)
我想知道我应该把这个电话放在Android服务类中吗?因为我的改装调用无论如何都是异步运行的?我见过的每个教程似乎都在课程或活动中运行它.我没见过有人使用过服务.我不是100%肯定目前最好的方法.谢谢