Back

android 中使用 service 的步骤

发布时间: 2017-03-18 02:17:00

参考:  https://developer.android.com/guide/components/services.html#StartingAService

1. 创建class: 

package topgroup.com.topgroupandroid;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class PreloadWebviewService extends Service {

    public static String LOG_TAG = "== PreloadWebviewService";


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d(LOG_TAG, "== in start command");

        Log.d(LOG_TAG, "before create layout");
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
        params.width = 0;
        params.height = 0;

        LinearLayout view = new LinearLayout(this);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        WebView wv = new WebView(this);
        wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        view.addView(wv);
        Log.d(LOG_TAG, "before load_url");
        wv.loadUrl("http://h5.topgroup.mobi");
        Log.d(LOG_TAG, "after load_url");
        windowManager.addView(view, params);
        Log.d(LOG_TAG, "after add view");

        // 10秒钟之后, 干掉它. 一般说来,这个函数放到 callback 中最合适. 
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Log.d(LOG_TAG, " now stop self");
                stopSelf();
            }
        }, 10000);

        return START_STICKY;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(LOG_TAG, "== in onCreate");
    }

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

2. 在对应的activity 中启动这个service

public class SplashActivity extends Activity {

    public String LOG_TAG = "== SplashActivity";

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash_activity);

        Log.d(LOG_TAG, "before start service");
        Intent intent = new Intent(this, PreloadWebviewService.class);
        startService(intent);
        Log.d(LOG_TAG, "after start service");

    }
}

3. 在 AndroidManifest.xml中声明.

application节点中:
<service android:name=".PreloadWebviewService" />
同时, 增加permission: 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Back