2017-03-23 31 views
0

更新 私は正常に実行されているサービスを開始しています。私はサービス内でBluetooth接続を維持しようとしています。 Bluetooth接続は、アプリケーションがアクティブなときにサービスを通じて正常に実行されています。しかし、アプリケーションを終了すると、サービスも停止し、Bluetooth接続が失われます。私の活動コードは以下の通りです。アプリケーションを終了した後にサービスが終了する

http://www.vogella.com/tutorials/AndroidServices/article.html

@Override 
protected void onResume() { 
    super.onResume(); 
    if (bluetoothService == null) { 
     kolamDeviceDialog(); 
    } else { 
     Log.e("MenuBluetoothService",bluetoothService.getStatus()+""); 
     if (!bluetoothService.getStatus()) { 
      kolamDeviceDialog(); 
     } 
    } 

} 

private void kolamDeviceDialog() { 
    MaterialDialog.Builder builder = new MaterialDialog.Builder(MenuActivity.this) 
      .title("Select Bot") 
      .items(scanTypes) 
      .itemsCallback(new MaterialDialog.ListCallback() { 
       @Override 
       public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { 
        if (position == 0) { 
         KolamDevice kolamDevice = new KolamDevice(); 
         Intent intent = new Intent(MenuActivity.this, BluetoothService.class); 
         intent.putExtra("Address", kolamDevice.getBigBotAddress()); 
         intent.putExtra("Name", kolamDevice.getBigBotName()); 
         startService(intent); 
         bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
        } else { 
         KolamDevice kolamDevice = new KolamDevice(); 
         Intent intent = new Intent(MenuActivity.this, BluetoothService.class); 
         intent.putExtra("Address", kolamDevice.getSmallBotAddress()); 
         intent.putExtra("Name", kolamDevice.getSmallBotName()); 
         startService(intent); 
         bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
        } 
       } 
      }); 
    builder.show(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // unbindService(mConnection); 
} 

private ServiceConnection mConnection = new ServiceConnection() { 

    public void onServiceConnected(ComponentName className, 
            IBinder binder) { 
     BluetoothService.MyBinder b = (BluetoothService.MyBinder) binder; 
     bluetoothService = b.getService(); 
     Toast.makeText(MenuActivity.this, "Connected", Toast.LENGTH_SHORT) 
       .show(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     bluetoothService = null; 
    } 
}; 

これが私のマニフェストファイルです。

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<application 
    android:name=".AppController" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:largeHeap="true" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" /> 
    <activity android:name=".ARCameraActivity" /> 
    <activity android:name=".RegistrationActivity" /> 
    <activity android:name=".LoginActivity" /> 
    <activity android:name=".SplashActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".MenuActivity" /> 
    <activity android:name=".ScanAndDrawActivity" /> 
    <activity android:name=".GalleryActivity" /> 
    <activity android:name=".PdfKolamActivity" /> 

    <service 
     android:enabled="true" 
     android:name=".BluetoothService"> 
     <intent-filter> 
      <action android:name="com.ignite.a01hw909350.kolamdemo.BluetoothService"/> 
     </intent-filter> 
    </service> 

    <receiver 
     android:name=".MyScheduleReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name=".MyStartServiceReceiver"/> 
</application> 

で最後にこれは私のサービスクラスです。

これは、明示的にstartService()を呼び出すReceiverです。

public class MyStartServiceReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent service = new Intent(context, BluetoothService.class); 
    context.startService(service); 
} 

}

私のコードでいただきました!間違った教えてください。

+0

「bindService」は何を返しますか? – pskink

+0

onServiceConnected()メソッドでは "connected"と言っていますが、バインドは起こっていません –

+0

'onServiceConnected'が呼び出された場合、あなたのサービスはあなたのサービスにバインドされています...' 'バインドはどういう意味ですか? '? – pskink

答えて

0

サービスをアクティビティにバインドしないで、代わりに開始済みサービスを使用します。 https://developer.android.com/guide/components/services.html

+0

私は活動とサービスの間を行き来するように縛る必要があります。 –

+0

その後、同時に開始されバインドされるサービスを作成します。 – ak0692

+0

そうですね。とにかく助けてくれてありがとう、 'startForeground()'を追加して動作させました。 –

関連する問題