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

ServiceSubclass如下:

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

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. <Button
  9. android:id="@+id/startButton"
  10. android:layout_width="200dip"
  11. android:layout_height="150dip"
  12. android:text="启动Service"
  13. />
  14. <Button
  15. android:id="@+id/stopButton"
  16. android:layout_width="200dip"
  17. android:layout_height="150dip"
  18. android:text="停止Service"
  19. />
  20. </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. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="8" />
  9. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="cn.testservice1.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. <!-- 注册服务 -->
  24. <service android:name="cn.testservice1.ServiceSubclass">
  25. <intent-filter >
  26. <action android:name="cc.test.com"/>
  27. </intent-filter>
  28. </service>
  29. </application>
  30. </manifest>

更多相关文章

  1. 解决Android应用安装快完毕时提示签名冲突
  2. Android安全机制(2) Android Permission权限控制机制
  3. 尽管在清单文件中指定了权限,但是ACCESS_FINE_LOCATION SecurityE
  4. 关于AndroidStudio中提示cannot resolve symble R,但程序可以正常
  5. Maven编译提示:软件包不存在
  6. Visual Studio 2012本身都是已经支持Jquery/Javascript智能提示
  7. 【JAVA】用java编写程序时总提示缺少方法主体或声明抽象的原因
  8. 如何修改JTextField (Swing)以显示在用户输入文本之前不会消失的
  9. Javascript 无提示框关闭IE窗口

随机推荐

  1. App抓包其实没那么复杂!Charles来帮你搞定
  2. 在同一基准下对前端框架进行比较[每日前
  3. Scrapy对接Docker
  4. 付费代理的使用
  5. Android金额输入框只允许输入小数点后两
  6. 为什么你不应该成为一个“数据科学“通才
  7. 内网主机从外面连接不了?SSH反向隧道来帮
  8. Scrapy框架的使用之Spider的用法
  9. Scrapy框架的使用之Scrapy入门
  10. 记一次网络中断故障处理