In one application (App1) I am broadcasting a message. This code below is correct -> the broadcast is detected if I try to get Broadcast in the same project.
sendBroadcast(new Intent("com.example.MESSAGE_INTENT").putExtra("MESSAGE", ((EditText) findViewById(R.id.textField)).getText()));
I created App2 which has a BroadcastReceiver
which waits for the broadcasted Intent
but the method onReceive
is never invoked.
How to change the BroadcastReceiver
app to make the service work in background all the time?
App2 manifest and code:
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("WORKS" , "!!!!!!!!!!");
Toast.makeText(context, "CAUGHTt\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG).show();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jjoe64" >
<permission-group
android:name="com.examples.my_permissions"
android:label="my permissions groupd" />
<permission
android:name="com.examples.my_permissions.MY_PERMISSION"
android:label="my permission"
android:permissionGroup="com.examples.my_permissions" />
<application>
<receiver
android:name="MyReceiver"
android:exported="true"
android:permission="com.examples.my_permissions.MY_PERMISSION" >
<intent-filter>
<action android:name="com.example.MESSAGE_INTENT" />
</intent-filter>
</receiver>
<service android:name="BackgroundService" />
</application>
</manifest>
Broadcaster MANIFEST
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:exported="true" >
<uses-permission android:name="com.examples.my_permissions.MY_PERMISSION" />
<permission-group
android:name="com.examples.my_permissions"
android:label="my permissions groupd" />
<permission
android:name="com.examples.my_permissions.MY_PERMISSION"
android:label="my permission"
android:permissionGroup="com.examples.my_permissions" />
<application>
<receiver
android:name="com.example.MyReceiver"
android:exported="true"
android:permission="com.examples.my_permissions.MY_PERMISSION" >
<intent-filter>
<action android:name="com.example.MESSAGE_INTENT" />
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
</application>
</manifest>
MAJOR EDIT
I have created an another application with MainActivity
there I have created final BroadcastReceiver
:
CODE:
package com.example.receiver2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
final BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("WORKS" , "!!!!!!!!!!");
Toast.makeText(context, "CAUGHTt\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.receiver2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="com.examples.my_permissions.MY_PERMISSION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission-group
android:name="com.examples.my_permissions"
android:label="my permissions groupd" />
<permission
android:name="com.examples.my_permissions.MY_PERMISSION"
android:label="my permission"
android:permissionGroup="com.examples.my_permissions" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.example.MyReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.example.MESSAGE_INTENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.receiver2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
BUT I GET AN EXCEPTION:
04-18 10:16:46.332: E/AndroidRuntime(1244): FATAL EXCEPTION: main
04-18 10:16:46.332: E/AndroidRuntime(1244): Process: com.example.receiver2, PID: 1244
04-18 10:16:46.332: E/AndroidRuntime(1244): java.lang.RuntimeException: Unable to instantiate receiver com.example.MyReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.MyReceiver" on path: DexPathList[[zip file "/data/app/com.example.receiver2-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.receiver2-1, /system/lib]]
04-18 10:16:46.332: E/AndroidRuntime(1244): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)