1

皆さんにとっては簡単な質問かもしれませんが、アプリを閉じたときにバックグラウンドで実行するサービスを作成するのは初めての試みです。私の問題は次のとおりです。「サービスの開始」ボタンをクリックしてもサービスが開始されません。トーストは表示されず、logcat にも何も表示されません (エラーもありません)。前もって感謝します!

サービスクラス

public class MyService extends Service {
private static final String TAG = "MyService";

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

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");

}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

}
}

私の主な活動

public void startServiceClick(View v){

    Log.d("foo", "onClick: starting srvice");
    startService(new Intent(this, MyService.class));
}

public void stopServiceClick(View v){

    Log.d("foo", "onClick: stopping srvice");
    stopService(new Intent(this, MyService.class));
}

マニフェスト xml

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

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>

<application
    android:icon="@drawable/maps1"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".LongitudeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DebugActivity" />
    <activity android:name=".GoogleMapsActivity" />

    <service android:enabled="true" android:name=".MyService" />

</application>

</manifest>
4

3 に答える 3

4
this is suggestion as all else looking fine 

次のような各関数で super を呼び出す必要がありますsuper.onCreate();

可能性としては、サービスが既に開始されている可能性があり、停止して再起動し、トーストが表示されることを確認してください....

試す

<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
于 2012-06-30T19:07:26.387 に答える
0

あなたのサービスをコードに追加しましたが、問題なく動作しています。以下のコードを参照して、あなたのものと一致させてください。

活動 >>

 public class TestActivity extends Activity implements OnClickListener {
   /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button bt = (Button)findViewById(R.id.button11);
    bt.setOnClickListener(this);
}


public void startServiceClick(View v){

    Log.d("foo", "onClick: starting srvice");
    startService(new Intent(this, MyService.class));
}

public void stopServiceClick(View v){

    Log.d("foo", "onClick: stopping srvice");
    stopService(new Intent(this, MyService.class));
}

@Override
public void onClick(View v) {
    startServiceClick(v);   

}

マニフェストファイル >>

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

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        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=".MyService" />

</application>

于 2012-06-30T19:46:59.460 に答える