Android在Service中弹出对话框(Dialog),即全局性对话框

时间:2022-11-01 15:51:05

先说具体做法,源代码在其后给出:
写好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如下:

 
  1. package cn.testservice1;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. /**
  10. * Demo描述:
  11. * 在服务中显示对话框
  12. *
  13. * 核心提示:
  14. * 为Dialog设置:
  15. * dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  16. *
  17. * 注意权限:
  18. * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  19. */
  20. public class MainActivity extends Activity {
  21. private Button mStartButton;
  22. private Button mStopButton;
  23. private Intent intent;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. init();
  29. }
  30. private void init(){
  31. intent=new Intent();
  32. intent.setAction("cc.test.com");
  33. //开启服务
  34. mStartButton=(Button) findViewById(R.id.startButton);
  35. mStartButton.setOnClickListener(new OnClickListener() {
  36. @Override
  37. public void onClick(View view) {
  38. startService(intent);
  39. }
  40. });
  41. //终止服务
  42. mStopButton=(Button) findViewById(R.id.stopButton);
  43. mStopButton.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View view) {
  46. stopService(intent);
  47. }
  48. });
  49. }
  50.  
  51. }

ServiceSubclass如下:

 
  1. package cn.testservice1;
  2.  
  3. import android.app.AlertDialog.Builder;
  4. import android.app.Dialog;
  5. import android.app.Service;
  6. import android.content.Intent;
  7. import android.os.IBinder;
  8. import android.view.WindowManager;
  9.  
  10. public class ServiceSubclass extends Service {
  11.  
  12. @Override
  13. public IBinder onBind(Intent arg0) {
  14. return null;
  15. }
  16. public void onCreate() {
  17. System.out.println("---> Service onCreate()");
  18. }
  19. @Override
  20. public void onStart(Intent intent, int startId) {
  21. super.onStart(intent, startId);
  22. System.out.println("---> Service onStart()");
  23. }
  24. @Override
  25. public int onStartCommand(Intent intent, int flags, int startId) {
  26. System.out.println("---> Service onStartCommand()");
  27. for (int i = 0; i < 10000; i++) {
  28. if (i==9527) {
  29. Builder builder=new Builder(getApplicationContext());
  30. builder.setTitle("Title");
  31. builder.setMessage("This is message");
  32. builder.setNegativeButton("OK", null);
  33. Dialog dialog=builder.create();
  34. dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  35. dialog.show();
  36. }
  37. }
  38. return super.onStartCommand(intent, flags, startId);
  39. }
  40. @Override
  41. public void onDestroy() {
  42. super.onDestroy();
  43. System.out.println("---> Service onDestroy()");
  44. }
  45.  
  46. }

 

main.xml如下:

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:gravity="center_horizontal"
  7. >
  8.  
  9. <Button
  10. android:id="@+id/startButton"
  11. android:layout_width="200dip"
  12. android:layout_height="150dip"
  13. android:text="启动Service"
  14. />
  15. <Button
  16. android:id="@+id/stopButton"
  17. android:layout_width="200dip"
  18. android:layout_height="150dip"
  19. android:text="停止Service"
  20. />
  21.  
  22. </LinearLayout>

AndroidManifest.xml如下:

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.testservice1"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="8" />
  10. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  11.  
  12. <application
  13. android:allowBackup="true"
  14. android:icon="@drawable/ic_launcher"
  15. android:label="@string/app_name"
  16. android:theme="@style/AppTheme" >
  17. <activity
  18. android:name="cn.testservice1.MainActivity"
  19. android:label="@string/app_name" >
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22.  
  23. <category android:name="android.intent.category.LAUNCHER" />
  24. </intent-filter>
  25. </activity>
  26. <!-- 注册服务 -->
  27. <service android:name="cn.testservice1.ServiceSubclass">
  28. <intent-filter >
  29. <action android:name="cc.test.com"/>
  30. </intent-filter>
  31. </service>
  32. </application>
  33.  
  34. </manifest>