我如何在Android中启动服务

时间:2012-10-23 13:48:02

标签: android android-service

我正在尝试了解Android中的Service类。

我创建了一个非常简单的活动:

public class ServiceLauncher extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_service_launcher);
    Intent intent = new Intent(this, NavOverrideService.class);
    startService(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_service_launcher, menu);
    return true;
}

}

此活动应该调用以下服务,但似乎没有显示我的日志或Toast消息:

   public class NavOverrideService extends Service{

    /* (non-Javadoc)
     * @see android.app.Service#onBind(android.content.Intent)
     */

    @Override
    public void onCreate() {
      // Start up the thread running the service.  Note that we create a
      // separate thread because the service normally runs in the process's
      // main thread, which we don't want to block.  We also make it
      // background priority so CPU-intensive work will not disrupt our UI.
        Log.v("Nav Service: ","Created");
        View disableStatusBar = new TableLayout(getApplicationContext());
        //mView = new HUDView(getApplicationContext());


         WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                50,
                 // This allows the view to be displayed over the status bar
               // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |  
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                 // this is to keep button presses going to the background window
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                 // this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                 // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                 PixelFormat.TRANSPARENT);

         handleParams.gravity = Gravity.TOP;
         handleParams.y = 750;

         //disableStatusBar.setBackgroundColor(Color.GREEN);
         disableStatusBar.setBackgroundResource(R.drawable.fake_nav);


         WindowManager winMgr = (WindowManager)getSystemService(WINDOW_SERVICE); 


         winMgr.addView(disableStatusBar, handleParams);


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        Log.v("Nav Service: ","started");

        // If we get killed, after returning from here, restart
        return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {
      Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
    }
}

我做错了什么?

哦,我的清单:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <service android:name="com.eliddell.services_test.NavOverrideService" />
    <activity
        android:name="com.eliddell.services_test.ServiceLauncher"
        android:label="@string/title_activity_service_launcher" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:2)

我创建了一个新项目。 这对我有用:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, ServiceTest.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


public class ServiceTest extends Service {

    @Override
    public void onCreate() {
        Log.v(null, "SERVICE TEST onCreate");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.v(null, "SERVICE TEST onStart");
        super.onStart(intent, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="ServiceTest"></service>
    </application>

</manifest>