首先要有一个不会退出的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>