I have:
MyApp extends Application with onCreate:
sendBroadcast(refreshAlarm);
Log.d(TAG, "broadcast sent with intent " + refreshAlarm);
Log.d(TAG, "onCreate");
where
static final Intent refreshAlarm = new Intent(ACTION_REFRESH_RECEIVER);
public static final String ACTION_REFRESH_RECEIVER = "com.example.myapp.REFRESH_RECEIVER";
BroadcastReceiver :
package com.example.myapp;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;
public class RefreshReceiver extends BroadcastReceiver
{
private static final String TAG = "RefreshReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "broadcast received with intent " + intent);
long interval = Long
.parseLong(PreferenceManager.getDefaultSharedPreferences(
context).getString("delay", "900")) * 1000;
PendingIntent operation = PendingIntent.getService(context, -1,
new Intent(context, RefreshService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(operation);
if (interval > 0)
{
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), interval, operation);
}
Log.d(TAG, "onReceive: delay = " + interval);
}
}
declared in manifes:
<receiver android:name="com.example.myapp.RefreshReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.myapp.REFRESH_RECEIVER" />
</intent-filter>
</receiver>
It seems to me that I have all I need to make this work. Broadcast is send in onCreate (I can see in log it is indeed send). Broadcast is declared with intent filter to receive refreshAlarm
intent, but it doesn't receive it and I cannot figure out why. Do I need anything else?
if you put BroadCastReceiver
in mainfest.xml you don't need to regiter it in code , register it in code only if you create it in your Code
this is example here :
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.mybroadcast" />
</intent-filter>
</receiver>
and here to call it from your class file ,
Intent intent = new Intent();
intent.setAction("android.mybroadcast");
sendBroadcast(intent);
It could be possible that its not working because your Receiver and the package name you are using for registering in your manifest do not match.
Make sure that your package name matches your respective package for your receiver.
try to register your broadcast receiver by programmatically
public void registerBroadcastReceiver(View view) {
this.registerReceiver(broadCastReceiver, new IntentFilter(
"com.example.myapp.REFRESH_RECEIVER"));
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT)
.show();
}
and unregister by this
public void unregisterBroadcastReceiver(View view) {
this.unregisterReceiver(broadCastReceiver);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT)
.show();
}
You said
MyApp extends Application
you should register MyApp as application in android manifest.
put this line in android manifest application element
android:name="your.package.MyApp"
Where your.pacakage is the package where MyApp file is located. Now your application element should look as follows
<application
android:name="your.package.MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/yourTheme" >
I had the same problem. It seems that the registered broadcastreceivers only work for system actions. With my custom action it only worked as long as my app was running (activity was in activity stack).
My workaround is to create an activity for it. This does not work in background, however it gives you the possibility to catch events from other apps
<activity android:name=".ImportToProActivity">
<intent-filter>
<action android:name="com.sourcecastle.logbook.ImportToProActivity"/>
</intent-filter>
</activity>
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.sourcecastle.freelogbook", "com.sourcecastle.logbook.ImportToProActivity"));
startActivity(intent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.