Dialog(七)——服务(Service)中弹出Dialog
来源:互联网 发布:linux wc l 编辑:程序博客网 时间:2024/05/16 19:53
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;@Overrideprotected 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() {@Overridepublic void onClick(View view) {startService(intent);}}); //终止服务 mStopButton=(Button) findViewById(R.id.stopButton); mStopButton.setOnClickListener(new OnClickListener() {@Overridepublic 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 {@Overridepublic IBinder onBind(Intent arg0) {return null;}public void onCreate() {System.out.println("---> Service onCreate()");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);System.out.println("---> Service onStart()");}@Overridepublic 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);}@Overridepublic 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>
0 0
- Dialog(七)——服务(Service)中弹出Dialog
- Service中弹出Dialog Toast
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- android在Service中弹出Dialog对话框
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- 在Service中弹出Toast和Dialog
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- 如何在service中弹出dialog
- Android在Service中弹出对话框(Dialog)
- Android在Service中弹出Dialog
- 在Service中弹出一个Dialog
- 在Service或者BroadCastReceiver中弹出dialog
- android service中弹出dialog对话框
- 红尘零落,勿忘心安!
- 赵又廷和高圆圆结婚了!
- 快速排序
- 当年惊艳的LG EnV系列全键盘手机,见过吗?
- 广州开展中心城区主要商圈户外广告统一规划
- Dialog(七)——服务(Service)中弹出Dialog
- 十一种通用滤波算法(转)
- 实例A引用实例B,java!
- 完整的头文件搜索规则是怎样的?
- Sina股票数据接口
- [转载/精华]JAVA中文显示乱码的原因
- Android ndk报错error: format not a string literal and no format arguments [-Werror=format-security]
- Qt_Quick的 Demos and Examples
- DB2 新建用户创建数据库流程