error at onHandleIntent cannot get receiver - android

I need to call onHandleIntent and receive a value from app1 and get result in app2 without user notices ( different apps, one activity will call the servixe of other app).
However, when onHandleIntent is starting in this line:
mReceiver = workIntent.getParcelableExtra("receiver");
Brings the error
E/Parcel: Class not found when unmarshalling: com.example.idscomercial.myapplication.AddressResultReceiver
java.lang.ClassNotFoundException: com.example.idscomercial.myapplication.AddressResultReceiver
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.idscomercial.myapplication.AddressResultReceiver" on path: DexPathList[[zip file "/data/app/com.veritran.vttokentest-2/base.apk"],nativeLibraryDirectories=[/data/app/com.veritran.vttokentest-2/lib/arm, /vendor/lib, /system/lib]]
I actually read :
https://stackoverflow.com/questions/28589509/android-e-parcel-class-not-found-when-unmarshalling-only-on-samsung-tab3
But still I cannot find the solution
This is the code:
App1
public class servicev extends IntentService {
protected ResultReceiver mReceiver;
public servicev() {
super("yeah");
}
#Override
protected void onHandleIntent(Intent workIntent) {
Toast b = Toast.makeText(this.getApplicationContext(), "onHandle starting", Toast.LENGTH_LONG );
b.show();
Log.d("just", "tellme");
String dataString = workIntent.getDataString();
mReceiver = workIntent.getParcelableExtra("receiver");
// mReceiver = new AddressResultReceiver(new Handler());
deliverResultToReceiver(1,"value of servicev");
}
private void deliverResultToReceiver(int resultCode, String message) {
Bundle bundle = new Bundle();
bundle.putString("receiver", message);
mReceiver.send(resultCode, bundle);
}
}
App2:
public class MainActivity extends AppCompatActivity {
protected ResultReceiver mResultReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startIntentService();
setContentView(R.layout.activity_main);
}
protected void startIntentService() {
//Intent intent = new Intent();
mResultReceiver = new AddressResultReceiver(new Handler());
Intent intent=new Intent("com.veritran.vttokentest.servicev.START_SERVICE");
intent.setPackage("com.veritran.vttokentest");
//Intent intent = new Intent(this, FetchAddressIntentService.class);
intent.putExtra("receiver", mResultReceiver);
startService(intent);
}
}
==> 2
public
class AddressResultReceiver extends ResultReceiver {
public AddressResultReceiver(Handler handler) {
super(handler);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
int duration = Toast.LENGTH_LONG;
String mAddressOutput = resultData.getString("1");
//Toast toast = Toast.makeText(context, "hi brow", duration);
//toast.show();
if (resultCode == 1) {
}
}
}
Manifest app1:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.veritran.vttokentest">
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.veritran.vttokentest.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:enabled="true"
android:exported="true"
android:name="com.veritran.vttokentest.servicev">
<intent-filter>
<action android:name="com.veritran.vttokentest.servicev.START_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>

There could be a couple things going on here. First you are going to need a receiver to ever get information back from your service to the main activity. To create a receiver, make a new class that looks like this:
public class myReceiver extends ResultReceiver {
private Receiver mReceiver;
public myReceiver(Handler handler) {
super(handler);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
public interface Receiver {
void onReceiveResult(int resultCode, Bundle resultData);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
Then, in your main activity, create the receiver onCreate() with the following code:
mReceiver = new myReceiver(new Handler());
mReceiver.setReceiver(this);
This will be necessary to actually get any data back. you will also need to pass the receiver into the intent as an extra intent.putExtra("receiver", mReceiver); in the section where you define the intent. Then, in the IntentService, you can send your info back to the MainActivity with workIntent.getParcelableExtra("receiver").send(1, b) (where 1 is the result code you want and b is a Bundle of all the info you want to send back).
As far as why your service is never being called, I would check here to see if you are actually sending a correct intent to the service. You should also try removing the intent-filter from your service as it is unnecessary.
Generally you are not going to want to create a new MainActivity inside the service and use the receiver instead. Otherwise your service appears to be correct so I would make sure you check the intent that you are trying to pass in. If that still fails there are a number of other threads here about this problem.
Android IntentService not starting
Reasons why an IntentService won't start? [closed]
I am new here so please let me know if you need more help.

Related

How to Send BroadCast from one app to another app

I have App A and App B. In App A I want to send broadcast to App B.
This is the code for App A:
final Intent intent = new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName", "code1id");
intent.setComponent(new ComponentName("com.pkg.AppB", "com.pkg.AppB.MainActivity"));
sendBroadcast(intent);
And in App B - In MainActivity, I have MyBroadCastReceiver Class.
public class MainActivity extends Activity {
private MyBroadcastReceiver MyReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broadcast from External App
IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
MyReceiver = new MyBroadcastReceiver();
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Data Received from External App", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(MyReceiver != null)
unregisterReceiver(MyReceiver);
}
}
I am getting the error - Receiver is not registered.
First thing first declare the receiver in app B in the manifest file like this:
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
when sending the broadcast add FLAG_INCLUDE_STOPPED_PACKAGES flag to the intent [src] because when you broadcast from app A to app B , app B might not be running, this flag insures that the broadcast reachs out even apps not running:
FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it
is sent to indicate that the intent is to be allowed to start a
component of a stopped application.
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
In your case it will be like this:
final Intent intent=new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(
new ComponentName("com.pkg.AppB","com.pkg.AppB.MyBroadcastReceiver"));
sendBroadcast(intent);
In App A: Send the broadcast here.
final Intent i= new Intent();
i.putExtra("data", "Some data");
i.setAction("com.pkg.perform.Ruby");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getApplicationContext().sendBroadcast(i);
In App B manifest
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
In App B MainActivity: register the receiver oncreate(), and unregister onDestroy()
public class MainActivity extends AppCompatActivity
{
private MyBroadcastReceiver MyReceiver;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
if(MyReceiver != null)
unregisterReceiver(MyReceiver);
}
}
In App B BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String data = intent.getStringExtra("data");
Log.i("BR" ,"Data received: " + data);
}
}
There may be two cases :
Your appB is not running, hence the activity is not instantiated, and so the receiver is not registered.
Your activity is destroyed, means that you have unregistered your receiver that you registered via registerReceiver() in onCreate()
Solution :
Register your broadcast receiver in manifest.
Inside manifest of appB :
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
And comment out the line in appA
intent.setComponent(new ComponentName("com.pkg.AppB","com.pkg.AppB.MainActivity"));
Write the logic in MyBroadcastReceiver to display relevant data/launch new activity
MyReceiver is class not object. Create
myReceiver = new MyReceiver();
and put...........
registerReceiver(myReceiver,intentFilter);
If this helps some one and it works for me
In App A in activity or in a content provider-
Intent intent = new Intent("Updated");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent (new
ComponentName "com.exam.appA",
"com.exam.appA.DbaseChanged"));
getContext().sendBroadcast(intent);
In App B in the manifest
<receiver
android:name=".DbaseChanged"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="Updated" />
</intent-filter>
</receiver>
In App B Broadcast receiver class-
public class DbaseChanged extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent
intent) {
String act = intent.getAction();
if(act != null && act.equals("Updated") )
{
Toast.makeText(context, act ,
Toast.LENGTH_SHORT).show();
}
}
}
I needed to call setPackage("package_name") to ensure explicitness when I registered the broadcast receiver in the Manifest. I was then able to receive the data even if the app was closed completely.
// sending app sends broadcast
Intent intent = new Intent(ACTION_RECOMMEND);
intent.putExtra(LISTEN_RECOMMENDATION, "Triggered - Jhene Aiko");
intent.putExtra(WATCH_RECOMMENDATION, "Goblin - Kim Go-eun");
intent.setPackage("com.example.package.receiverapp");
sendBroadcast(intent);
//receiving app manifest registers receiver
<receiver
android:name=".ManifestRegisteredBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.random.action.RECOMMEND" />
</intent-filter>
</receiver>
I didn't need to add intent.setPackage(package_name) when registering the receiver via an activity, but this also meant that I couldn't get the data if the activity was destroyed (app closed, app in background for long period)

Starting Android application at boot completion: is my solution overly complicated?

I have an application that I would like to have automatically start following boot completion. The following code seems overly complicated and I get erratic application starts when swiping to a neighbouring workspace.
What am I missing here? I have an activity class, a service class, as well as a broadcast receiver. Below is my code (in that order) followed by the manifest.
public class BlueDoor extends Activity implements OnClickListener{
Button btnExit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
btnExit = (Button) this.findViewById(R.id.ExitButton);
btnExit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ExitButton:
System.exit(0);
break;
}
}
}
service.class
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(this, BlueDoor.class);
startActivity(callIntent);
// do something when the service is created
}
}
broadcast receiver
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluedoor"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".StartBlueDoorAtBootReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".BlueDoorStartService" >
</service>
<activity
android:name=".BlueDoor"
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>
UPDATE Solution(s), 10/22/2015:
Changing the service to:
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
and the receiver to:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Start Service On Boot Start Up
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
//Start App On Boot Start Up
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
resulted in a working configuration using a service w/no misbehaving. However deleting the service all together and modifying the receiver thus:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
also resulted in a functional as well as a more concise configuration that starts the application following boot completion.
Your BroadcastReceiver calls
context.startService(serviceIntent)
so the service will be created if it doesn't exist yet (which will be the case shortly after booting) and thus start the activity from its onCreate() method. So the app works, to a certain extent.
BUT when you call startService(), the system always calls the service's onStartCommand() method. You did not override that method, so the system uses the standard implementation from class android.app.Service.
As you can read on grepcode.com, the method will return a value like START_STICKY by default. This tells the system to keep the service alive until it is explicitly stopped.
In your case, I suppose the system reacted to the swiping by temporarily killing and then reanimating (= creating) the service, which in turn started your activity.
Some information on the service lifecycle can be found here.
What you can do:
Override onStartCommand() to start the activity from there instead of from onCreate(). Then use stopSelf(int) like described here
One last thing: when exiting from the activity, don't use System.exit(0) but call finish() instead, see this SO answer for "why".

BroadcastReceiver onReceive() not Called when registered dynamically

The function "onReceive" is called when BroadcastReceiver is Registered in the Manifest but NOT called if registered dynamically.
The code that works is below:
public class EyeGesture extends BroadcastReceiver {
//Eye Gesture
private static IntentFilter eyeGestureIntent;
private static Context eyeGestureContext;
private static StringBuilder gestureInfo = null;
private static BroadcastReceiver broadcastReceiver;
// public void startEyeListening() {
//Eye Gesture
//}
#Override
public void onReceive(Context context, Intent intent) {
// this = context;
if (intent.getStringExtra("gesture").equals("WINK")) {
Log.e("WINKED ","");
}else {
Log.e("SOMETHING", "is detected " + intent.getStringExtra("gesture"));
}
//Disable Camera Snapshot
// abortBroadcast();
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntent = null;
eyeGestureContext = null;
gestureInfo = null;
}
}
Below is the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.inno.glassplugin" >
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainFunct"
android:icon="#drawable/ic_glass_logo"
android:label="#string/title_activity_main_funct" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger" />
</activity>
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
</application>
</manifest>
The problem is that "onReceive" is NOT called when registered dynamically. I have to do this in a dynamic way.
Below is the code that is NOT working code.
public class EyeGesture extends Activity {
//Eye Gesture
IntentFilter eyeGestureIntentFilter;
Context eyeGestureContext;
BroadcastReceiver broadcastReceiver;
public EyeGesture(){
Log.e("CONSTRUCTOR ", "");
eyeGestureContext = MainFunct.getCurrentContext();
eyeGestureIntentFilter = new IntentFilter("com.google.glass.action.EYE_GESTURE");
eyeGestureIntentFilter.setPriority(1000);
startRunning();
}
void startRunning(){
eyeGestureContext.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Received ", " Something");
}
},eyeGestureIntentFilter);
}
#Override
public void onResume(){
super.onResume();
}
#Override
public void onPause(){
super.onPause();
unregisterReceiver(broadcastReceiver);
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntentFilter = null;
eyeGestureContext = null;
}
}
Also, I don't want to extend BroadcastReceiver from this class. Why am I not receiving anything if registered dynamically. I also removed the following line from the Manifest:
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
but still, it is not working. There is no error or exception thrown.
What am I doing wrong?
Are you using explicit intent? It seems that dynamically registered broadcast receivers cannot receive explicit intents. Implicit intents work.
For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
If the issue is not explicit intents but if you are using LocalBroadcastManager for sendBroadcast then make sure that the registerReceiver is also called of LocalBroadcastManager and not of Context
Try using ApplicationContext instead of Activity.
Modyifing line:
eyeGestureContext = MainFunct.getCurrentContext();
I would try things in this order:
eyeGestureContext = getApplicationContext();
eyeGestureContext = getApplication();
If above does not work I would extend the Application and do:
public class MyExtendedApplication extends Application {
private static MyExtendedApplication instance;
public static MyExtendedApplication getInstance() {
return instance;
}
}
This works for me with global "android.net.conn.CONNECTIVITY_CHANGE" broadcast
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
so should also for you with "com.google.android.glass.action.EYE_GESTURE"
Watching adb logcat in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never hits the event bus; instead, it skips straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button. So it looks like the eye-gesture API was removed without announcement.
The receiver should extend the BroadcastReceiver class.
Define the receiver in the manifest
In the code (maybe onCreate), register the receiver
Create a receiver object
Define the intent filters
call RegisterReceiver() passing in the receiver and the intent filters

Broadcastreceiver missing Extra data from Intent

I have a Problem with my Broadcastreceiver and I have no idea what I am doing wrong.
I already read many posts #stackoverflow, but nothing helped.
I am sending a broadcast from my helperclass, the broadcastreceiver is implemented in an activity. I am putting an extra on the Intent at the helperclass before sending it, but the intent # the activity has null extras. What am I doing wrong?
This is my sourcecode:
//Helperclass
...
public static String BROADCAST_ACTION_DM = "de.je.toctohk.DISPLAYMESSAGE";
...
public void method() {
String msg = _intent.getStringExtra("message");
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION_DM);
broadcast.putExtra("msg", _chatentryid);
sendStickyBroadcast(broadcast);
}
//Activity
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String test = getIntent().getStringExtra("msg");
Toast.makeText(getApplicationContext(), intent.getExtras().getString("msg"), Toast.LENGTH_SHORT).show();
}
};
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(GcmIntentService.BROADCAST_ACTION_DM);
_testintent = registerReceiver(receiver, filter);
super.onResume();
}
//Manifest
<activity
android:name="de.je.toctohk.activities.ChatActivity"
android:label="#string/title_activity_push" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="de.je.toctohk.DISPLAYMESSAGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
What I already tried:
normal broadcast
only registering the filter in the Activity
only registering the filter in the Manifest
Sticky broadcast
context.getIntent().getExtra…
intent.getExtra…
It would be great if somebody could give me some advises.
Thanks a lot!!

Pass a string from an Android Java activity to a broadcast receiver

I have spent the last couple of hours looking through other questions on this topic and none that I found were able to give me any answers.
What is the best way to pass a string from an activity to a broadcast receiver in the background?
Here is my main activity
public class AppActivity extends DroidGap {
SmsReceiver mSmsReceiver = new SmsReceiver();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll;
scroll = new ScrollView(this);
Bundle bundle = getIntent().getExtras();
final String ownAddress = bundle.getString("variable");
registerReceiver(mSmsReceiver, new IntentFilter("MyReceiver"));
Intent intent = new Intent("MyReceiver");
intent.putExtra("passAddress", ownAddress);
sendBroadcast(intent);
Log.v("Example", "ownAddress: " + ownAddress);
}
}
Here is my broadcast receiver
public class AppReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
final String ownAddress = intent.getStringExtra("passAddress");
Toast test = Toast.makeText(context,""+ownAddress,Toast.LENGTH_LONG);
test.show();
Log.v("Example", "ownAddress: " + ownAddress);
}
}
Here is the manifest for my receiver
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When the broadcast receiver logs an event the app crashes. I need to have it run behind the scenes and pull a string from my main activity.
Can anyone help me with this or point me in the right direction?
Addition from comments and chat
Your String ownAddress will always be null unless the Intent has an String with the key passAddress in the the Bundle extras. Anytime your Receiver catches an Intent (whether from SMS_SENT, SMS_RECEIVED, or BOOT_COMPLETED) ownAddress will be null because the OS doesn't provide a String extra named passAddress. Hope that clears things up.
Original Answer
I haven't used DroidGap but this is what you want for a regular Android Activity.
Activity:
public class AppActivity extends Activity {
AppReceiver mAppReceiver = new AppReceiver();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(mAppReceiver, new IntentFilter("MyReceiver"));
String string = "Pass me.";
Intent intent = new Intent("MyReceiver");
intent.putExtra("string", string);
sendBroadcast(intent);
}
}
Receiver:
public class AppReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getStringExtra("string"), Toast.LENGTH_LONG).show();
}
}
Don't forget to unregister the receiver in onDestroy(), like this:
#Override
protected void onDestroy() {
unregisterReceiver(mAppReceiver);
super.onDestroy();
}

Categories

Resources