0

这是我的 WallePapeAuto.java 代码:

    package com.demo.wallpaper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


public class WallePapeAuto extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent(WallePapeAuto.this, BackgroundService.class);
        startService(intent);
    }

}

这是我的 BackgroundService.java 代码:

    package com.demo.wallpaper;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BackgroundService extends Service{

    final String TAG = "BackgroundService";
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId){
        Log.i(TAG,"onStart launched");
        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Log.i(TAG,"timerTask launched");
                checkStatus();

            }       
        };

        timer.scheduleAtFixedRate(timerTask,15000,15000); //call after every 15 sec
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        Log.i(TAG,"onCreate");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.i(TAG,"onDestroy");
        super.onDestroy();

    }


    public void checkStatus(){

        try{
            Log.i(TAG,"in checking status...");
        } catch (Exception e){
            Log.i(TAG, "Error occur during checking status");
            e.printStackTrace();
        }
    }



}

我的 AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.wallpaper"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.wallpaper.WallePapeAuto"
            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:enabled="true" 
                 android:name=".BackgroundService" />
    </application>

</manifest>

我希望它在运行时会显示一些日志值,但我的 Logcat 中没有任何内容。我不确定我做错了哪一部分。任何帮助我将不胜感激。提前致谢。

4

1 回答 1

0

我认为您必须将服务类放置manifest如下:

 <service android:name="BackgroundService">
        <intent-filter>
        <action android:name="com.yourpackage.BackgroundService" />
        </intent-filter>
    </service>
于 2013-01-21T07:35:28.813 回答