先说具体做法,源代码在其后给出:
写好Alter功能块后,在alter.show()语句前加入:
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
注:alter为AlertDialog类型对象
然后在AndroidManifest.xml中加入权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
点击进入: 品牌服饰鞋包9.9元特价包邮抢购:http://shop109590806.taobao.com/
下面进行简单的解释:
如果只在Service中写入常在Activity中使用的创建Alter的代码,运行时是会发生错误的,因为Alter的显示需要依附于一个确定的Activity类。而以上做法就是声明我们要弹出的这个提示框是一个系统的提示框,即全局性质的提示框,所以只要手机处于开机状态,无论它现在处于何种界面之下,只要调用alter.show(),就会弹出提示框来。
MainActivity如下:
- package cn.testservice1;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.app.Activity;
- import android.content.Intent;
- /**
- * Demo描述:
- * 在服务中显示对话框
- *
- * 核心提示:
- * 为Dialog设置:
- * dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- *
- * 注意权限:
- * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
- */
- public class MainActivity extends Activity {
- private Button mStartButton;
- private Button mStopButton;
- private Intent intent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- intent=new Intent();
- intent.setAction("cc.test.com");
- //开启服务
- mStartButton=(Button) findViewById(R.id.startButton);
- mStartButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- startService(intent);
- }
- });
- //终止服务
- mStopButton=(Button) findViewById(R.id.stopButton);
- mStopButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- stopService(intent);
- }
- });
- }
- }
ServiceSubclass如下:
- package cn.testservice1;
- import android.app.AlertDialog.Builder;
- import android.app.Dialog;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.view.WindowManager;
- public class ServiceSubclass extends Service {
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- public void onCreate() {
- System.out.println("---> Service onCreate()");
- }
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- System.out.println("---> Service onStart()");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- System.out.println("---> Service onStartCommand()");
- for (int i = 0; i < 10000; i++) {
- if (i==9527) {
- Builder builder=new Builder(getApplicationContext());
- builder.setTitle("Title");
- builder.setMessage("This is message");
- builder.setNegativeButton("OK", null);
- Dialog dialog=builder.create();
- dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- dialog.show();
- }
- }
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- System.out.println("---> Service onDestroy()");
- }
- }
main.xml如下:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- >
- <Button
- android:id="@+id/startButton"
- android:layout_width="200dip"
- android:layout_height="150dip"
- android:text="启动Service"
- />
- <Button
- android:id="@+id/stopButton"
- android:layout_width="200dip"
- android:layout_height="150dip"
- android:text="停止Service"
- />
- </LinearLayout>
AndroidManifest.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.testservice1"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="8" />
- <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="cn.testservice1.MainActivity"
- 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="cn.testservice1.ServiceSubclass">
- <intent-filter >
- <action android:name="cc.test.com"/>
- </intent-filter>
- </service>
- </application>
- </manifest>