Why is not my BroadcastReceiver capturing events?

0

I try to restart a servicio in case it is destroyed. First I create my servicio :

 public class MyIntentService extends Service {
    WebSocketClient mWebSocketClient;
    URI uri;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            uri = new URI("ws://websockethost:8080");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        mWebSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Log.i("Websocket", "Opened");
                mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
            }

            @Override
            public void onMessage(String s) {
                mWebSocketClient.onMessage(s);
                if(s.equals("s")){
                    //notificacion.....
                }
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                Log.i("Websocket", "Closed " + s);
                transfer();
            }

            @Override
            public void onError(Exception e) {
                Log.i("Websocket", "Error " + e.getMessage());

            }

        };
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        mWebSocketClient.connect();
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void transfer(){
        String code ="half";
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        intent.putExtra("data",code);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent("com.example.myapplication.MyIntentService");
        intent.putExtra("Value", String.valueOf(mWebSocketClient));
        sendBroadcast(intent);
    }
}

In the method onDestroy of servicio sent information to my BroadcastReceiver :

    public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Log.i("Servicio", "El servicio se detuvo");
            context.startService(new Intent(context, MyIntentService.class));

        }
    }
}

Then in my method OnResume of my MainActivity I recover a String that I am sending as a test to verify that the Servicio is actually running by means of the BroadcastReceiver , (I tried without the BroadcastReceiver starting my normal service in the method onCreate of MainActivity and if I received the data), here my MainActivity :

    public class MainActivity extends AppCompatActivity {

    public String signal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //startService(new Intent(this,MyIntentService.class));

    }

    @Override
    protected void onResume() {
        super.onResume();
        //stopService(new Intent(this,MyIntentService.class));
        signal = getIntent().getStringExtra("data");
        if (signal!=null){
            Toast.makeText(getApplicationContext(),"datos recibidos\n"+signal,Toast.LENGTH_SHORT).show();
        }
    }

}

And finally record the necessary in my AndroidManifest :

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<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">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="com.example.myapplication.MyIntentService" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name=".MyIntentService"
        android:exported="false" />
</application>

Now using the BroadcastReceiver I do not run the servicio , since I am not receiving the data I should receive using onResume , and if I try to start servicio in OnCreate of MainActivity the App does not open.

    
asked by David Villegas 07.08.2018 в 17:55
source

0 answers