后退按钮后,我的服务站 [英] My service stop after the back button

查看:110
本文介绍了后退按钮后,我的服务站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个沟通的服务和活动。 当我按一下按钮(我有银河S3只有一个按钮),那么我当然disapear活动,我的服务继续运行,但如果我点击后退(触摸)按钮,然后我的服务被销毁。 我怎样才能改变这种状况?我想该服务继续运行,直到活动毁坏它。

修改

下面是code:

服务:     公共类为MyService延伸服务     {         私有静态最后字符串变量=BroadcastService;         公共静态最后弦乐BROADCAST_ACTION =com.websmithing.broadcasttest.displayevent;         私人最终处理程序处理程序=新的处理程序();         私人意图的意图;         INT计数器= 0;

  @覆盖
    公共无效的onCreate()
    {
        super.onCreate();
        意图=新的意图(BROADCAST_ACTION);
    }

    @覆盖
    公共无效ONSTART(意向意图,诠释startId)
    {
       // handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI,1000); //1秒

    }

    私人可运行sendUpdatesToUI =新的Runnable(){
        公共无效的run(){
            DisplayLoggingInfo();
            handler.postDelayed(这一点,1000); //10秒
        }
    };

    私人无效DisplayLoggingInfo(){
        intent.putExtra(反,将String.valueOf(++计数器));
        sendBroadcast(意向);
    }

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    @覆盖
    公共无效的onDestroy(){
        handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
    }
}
 

活动:

 公共类MainActivity延伸活动{
    私有静态最后字符串变量=BroadcastTest;
    私人意图的意图;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        意图=新的意图(这一点,MyService.class);

        startService(意向);
        registerReceiver(BroadcastReceiver的,新的IntentFilter(MyService.BROADCAST_ACTION));
    }

    私人的BroadcastReceiver的BroadcastReceiver =新的BroadcastReceiver(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            的updateUI(意向);
        }
    };


    @覆盖
    公共无效的onDestroy(){
        super.onPause();
        unregisterReceiver(BroadcastReceiver的);
        stopService(意向);
    }

    私人无效的updateUI(意向意图)
    {
        串计数器= intent.getStringExtra(反);
        Log.d(TAG,计数器);

        TextView的txtCounter =(TextView中)findViewById(R.id.textView1);
        txtCounter.setText(柜);
    }
}
 

解决方案

Ofcourse您服务停止,当你preSS后退按钮。后退按钮大多数呼叫完成()的活动,而且它被摧毁。当你preSS另一个按钮(home键),它只是减少了你的应用程序,它会被后来才摧毁,当OS要释放空间。

如果你想保持你的服务运行,使之成为前台服务,并且不停止在活动销毁。

I have a service and an activity that communicate. When I click the button(I have galaxy s3 there is only one button) then my activity of course disapear and my service is keep running but if I click the back (touch) button then my service is destroyed. How can I change that?I want the service to keep running untill the activity destroys it.

EDIT

Here is the code:

Service: public class MyService extends Service { private static final String TAG = "BroadcastService"; public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent"; private final Handler handler = new Handler(); private Intent intent; int counter = 0;

    @Override
    public void onCreate() 
    {
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);  
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
       // handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

    }

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            DisplayLoggingInfo();           
            handler.postDelayed(this, 1000); // 10 seconds
        }
    };    

    private void DisplayLoggingInfo() {
        intent.putExtra("counter", String.valueOf(++counter));
        sendBroadcast(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {       
        handler.removeCallbacks(sendUpdatesToUI);       
        super.onDestroy();
    }       
}

Activity:

public class MainActivity extends Activity {
    private static final String TAG = "BroadcastTest";
    private Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(this, MyService.class);

        startService(intent);
        registerReceiver(broadcastReceiver, new IntentFilter(MyService.BROADCAST_ACTION));
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateUI(intent);       
        }
    };    


    @Override
    public void onDestroy() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
        stopService(intent);        
    }   

    private void updateUI(Intent intent) 
    {
        String counter = intent.getStringExtra("counter"); 
        Log.d(TAG, counter);

        TextView txtCounter = (TextView) findViewById(R.id.textView1);
        txtCounter.setText(counter);
    }
}

解决方案

Ofcourse your service stops, when you press the back button. The back button most calls finish() on the activity, and it is destroyed. When you press the other button (the home button), it just minimizes your app, and it will be only destroyed later, when the OS wants to free up space.

If you want to keep your service running, make it a foreground service, and dont stop it on activity destroy.

这篇关于后退按钮后,我的服务站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆