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
原创粉丝点击