android 在界面上添加一不会消失的按键


首先要有一个不会退出的service

service source: 


package jian.test.AlwaysShowButton;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class LocalService extends Service {
	private Context mContext ;
	private static final String TAGs = "showService"; 
	 private LinearLayout mLayout;
	 private Button mButton;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate(){
		Log.i(TAGs,"onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnCreate");
		super.onCreate(); 
		
		LayoutInflater flater = LayoutInflater.from(this);
		mContext = this.getApplicationContext();
		WindowManager localWindowManager = (WindowManager) this.mContext.getSystemService(WINDOW_SERVICE);
	    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
	    View view = flater.inflate(R.layout.button_layout, null);
	    mLayout = (LinearLayout) view.findViewById(R.id.layout1);
	    mButton = (Button)view.findViewById(R.id.button1);
	    localLayoutParams.height = 60;
	    localLayoutParams.width = 60;
	    localLayoutParams.flags = 40;
	    localLayoutParams.format = PixelFormat.TRANSPARENT;
	    localLayoutParams.alpha= (float) 0.5;
	    localLayoutParams.type = 2003;
	    localLayoutParams.y = Gravity.CENTER;
	    mButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i(TAGs,"onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnCreate =" +v.getId());
				
			}
	    	
	    });
	    Log.i(TAGs,"onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnCreate =" +mLayout.getHeight() +" w = "+ mLayout.getWidth());
	    localWindowManager.addView(mLayout, localLayoutParams);
	}

}



activity source : 任务就是start service ,当然也可以让service自己启动。

package jian.test.AlwaysShowButton;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class AlwaysShowButtonActivity extends Activity {
    /** Called when the activity is first created. */
	 private static final String TAG = "AlwaysShowButtonActivity";
	 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startService(new Intent("showService"));
    }
}

简单的layout

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

button_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/layout1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<Button android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="testButton"
    />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="jian.test.AlwaysShowButton"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlwaysShowButtonActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

		<service android:name="LocalService"
				  android:permission="android.permission.SYSTEM_ALERT_WINDOW">
        	<intent-filter>
                <action android:name="showService" />
                <category android:name="android.intent.category.default" />
        	</intent-filter>
		</service>
    </application>
</manifest>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值