从服务中显示布局

时间:2012-03-26 07:52:59

标签: android android-layout

我想在首次运行服务时启动一个活动,显示一个布局。

我有以下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.display.helper"
      android:versionCode="1"
      android:versionName="0.1.124">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.TELEPHONY"/>
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".GpsTrackActivity" android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

      </intent-filter>
    </activity> 
       <service android:name=".dhaService" >
       <intent-filter>
       <action
       android:name = "com.display.helper.dhaService">
       </action>
       </intent-filter>
       </service>
       <receiver android:name=".dha">
       <intent-filter>
       <action
       android:name ="android.intent.action.BOOT_COMPLETED">
...

我在StartService方法中执行此操作:

public class dhaService extends Service 
 {
    @Override
    public ComponentName startService(Intent service) {
        // TODO Auto-generated method stub
        Notification note = new Notification(R.drawable.icon,"sadasd",System.currentTimeMillis());
        note.setLatestEventInfo(this, "Tracking device", "Tracking", null);
        note.flags |= Notification.FLAG_NO_CLEAR;
        this.startForeground(1212,note);
        Intent dialogIntent = new Intent(getBaseContext(),GpsTrackActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(dialogIntent);
        return super.startService(service);

    }

布局永远不会显示我做错了什么?

1 个答案:

答案 0 :(得分:1)

documentation说:“始终会创建一个对话框,并将其显示为活动的一部分。”我认为你需要激活一个Activity(可能没有常规屏幕)并覆盖onCreateDialog(int)回调。