0

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;
}

}

このアクティビティは次のサービスを呼び出すことになっていますが、ログまたはトーストメッセージが表示されていないためではないようです。

   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>
4

1 に答える 1

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>
于 2012-10-23T14:12:41.827 に答える