在 Android 中创建后台服务 [英] Creating Background Service in Android

查看:58
本文介绍了在 Android 中创建后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我需要在 android 中创建一个服务.我可以像这样注册服务:

In my project I need to create a service in android. I am able to register the service like this :

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

   <service   android:enabled="true"
    android:name=".ServiceTemplate"/>
      <activity
        android:name=".SampleServiceActivity"
        android:label="@string/app_name" >
        <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
</application>

我在如下活动中调用此服务:-

I am calling this service inside an activity like below:-

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent service = new Intent(getApplicationContext(), ServiceTemplate.class);
    this.startService(service);
}

但是如果我杀死当前的活动,服务也会被破坏.我需要这个服务总是在后台运行.我需要做什么?如何注册服务?如何启动服务?

But if I kill the current activity , the service is also destroyed. I need this service always running in the background. What I need to do? How do I register the service? How do I start the service?

推荐答案

这里有一种半不同的方式来保持服务永远运行.如果您愿意,可以通过代码杀死它

Here is a semi-different way to keep the service going forever. There is ways to kill it in code if you'd wish

后台服务:

package com.ex.ample;

import android.app.Service;
import android.content.*;
import android.os.*;
import android.widget.Toast;

public class BackgroundService extends Service {

    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();

        handler = new Handler();
        runnable = new Runnable() {
            public void run() {
                Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
                handler.postDelayed(runnable, 10000);
            }
        };

        handler.postDelayed(runnable, 15000);
    }

    @Override
    public void onDestroy() {
        /* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
        //handler.removeCallbacks(runnable);
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
    }
}

以下是您从主要活动或任何您希望的地方开始的方式:

Here is how you start it from your main activity or wherever you wish:

startService(new Intent(this, BackgroundService.class));

onDestroy() 将在应用程序关闭或终止时被调用,但 runnable 会立即启动它.您还需要删除处理程序回调.

onDestroy() will get called when the application gets closed or killed but the runnable just starts it right back up. You need to remove the handler callbacks as well.

我希望这对某人有所帮助.

I hope this helps someone out.

有些人这样做的原因是因为公司应用程序在某些情况下用户/员工不能停止某些事情:)

The reason why some people do this is because of corporate applications where in some instances the users/employees must not be able to stop certain things :)

http://i.imgur.com/1vCnYJW.png

这篇关于在 Android 中创建后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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