0

我想创建一个无论正在运行的应用程序如何都始终显示的图标。

我已经读过这个问题

我的服务永远不会被调用。我错过了什么吗?

我的主要活动:

protected void onCreate(Bundle savedInstanceState) {
//  startActivity(new Intent(this, HUD.class));
    Intent Servintent = new Intent("com.sample.screen.HUD");  
    this.startService(Servintent);

我的服务类:

package com.example.screen;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

public class HUD extends Service{
    HUDView mView;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        String s;
        s = "Hari";

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
        mView = new HUDView(this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
        if(mView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
            mView = null;
        }
    }
}

HUD视图:

package com.example.screen;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Toast;

public class HUDView extends ViewGroup{
    private Paint mLoadPaint;
    public HUDView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
          Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show();
            mLoadPaint = new Paint();
            mLoadPaint.setAntiAlias(true);
            mLoadPaint.setTextSize(10);
            mLoadPaint.setARGB(255, 255, 0, 0);
    }   
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText("Hello World", 5, 15, mLoadPaint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(event);
        Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
        return true;
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

    }

}

安卓清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <service android:name=".HUD" ></service>

为什么我的服务永远不会被调用?请帮我。

4

1 回答 1

1

像这样创建意图

Intent Servintent = new Intent(Mainactivity.this , HUD.class);  
this.startService(Servintent);
于 2013-07-23T10:06:32.073 回答