StartService()でサービスをアクティブにしようとしていますが、onStartCommand()関数の横にある必要があります。 私はonStartCommand()関数を使用しないために多くの方法を試しましたが、私のサービスのインテントからの情報が必要です。マニフェストStartService()関数はアクティブではありません(マニフェストが正しい)
DBHelper dbHelper;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
startServiceThread();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
if (extras != null) {
dbHelper = (DBHelper)extras.get("dbHelper");
}
return super.onStartCommand(intent, flags, startId);
}
: (ここで私はまた、単に名前でサービスを追加しようとしました= "MYSERVICE。"
dbHelper = new DBHelper(this);
listTasks = (ListView)findViewById(R.id.list_todo);
loadTaskList();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
eventIntent = new Intent(view.getContext(), NewEventActivity.class);
startActivityForResult(eventIntent, ADD_EVENT_REQUEST);
}
});
Intent serviceIntent = new Intent(this, MyService.class);
serviceIntent.putExtra("dbHelper", (Parcelable)dbHelper);
if (!isMyServiceRunning(MyService.class))
{
startService(serviceIntent);
}
}
//gets any service class and check if its alive (running)
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
たMyService: - OnCreateの
MainAcitivty:ここ コードですしかし、何も変わらなかった)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity android:name=".NewEventActivity" />
<activity android:name=".LockApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.user.project.MyService" />
</application>
"しかし、これはどうしたのですか?あなたは不要な 'isMyServiceRunning()'コードを取り除こうとしましたか? 'startServiceThread()'でおそらくクラッシュしていますか? 'super.onCreate()'の前にそのコードを実行していることを考慮していますか? – CommonsWare
あなたのケースでは 'startService'は何を返していますか? – pskink
isMyServiceRunning()コードは正常に動作しますが、 @CommonsWare関数startService()が呼び出されます。 startServiceThread()は、インテントから情報を取得できないため、クラッシュします。とにかく、onStartCommand()はstartService()の後を呼び出しません。デバッガでは、startSerivce()は実行されますが、onStartCommand()は実行されません。 –