先說具體做法,源代碼在其後給出:
寫好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>