Android:应用程序的图标始终位于顶部

时间:2013-07-23 10:03:06

标签: android service

我想创建一个始终显示的图标,而不管正在运行的应用程序如何。

我已经阅读了这个question

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

我的主要活动:

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;
        }
    }
}

HUDView:

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

    }

}

Android Manifest:

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

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

1 个答案:

答案 0 :(得分:1)

像这样创建意图

Intent Servintent = new Intent(Mainactivity.this , HUD.class);  
this.startService(Servintent);