I am having an issue where my application is constantly entering the onReceive method and I am not sure why. I entered some log statements but I still do not understand why the onReceive is being called. I did some research looking around, just trying to find examples of using BroadcastReceiver and AlarmManager, and I followed this sample project, and found many other posts about users not being able to receive broadcasts, but none about users receiving too many.
Here is some of my code:
AlarmClock.java:
public class AlarmClock extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
Log.d("TAG", "Signal Recieved");
SetAlarm(context);
wl.release();
}
public void SetAlarm(Context context){
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 32000, pi);
Log.d("TAG", "Alarm Set");
}
}
AlarmService.java:
public class AlarmService extends IntentService {
public AlarmService(){
super("AlarmService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("TAG", "onHandleIntent");
//operation to be completed
}
}
AndroidManifest.XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.allen.repeatdialer" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
<receiver android:name=".AlarmClock">
<intent-filter>
<action android:name="com.example.allen.repeatdials.CALL_PLACED"/>
</intent-filter>
</receiver>
<service android:name=".AlarmService"></service>
</application>
...
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
The application will only display Log.d("TAG", "onHandleIntent"); when I call sendBroadcast(), but the other log statements are displayed every 32 seconds. But also, after I call sendBroadcast() once, Log.d("TAG", "onHandleIntent"); will also be displayed every 32 seconds.
EDIT(Code that calls sendBroadcast()):
public class HomeScreen extends Activity implements OnClickListener {
//*******************************************************
//* On Create *
//*******************************************************
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
}
...
@Override
public void onClick(View arg0){
switch (arg0.getId()){
case R.id.bContact:
displayContacts(Names, IDs);
Log.d("TAG", "Contacts Pressed");
break;
case R.id.bStartDial:
Log.d("TAG", "Dial Pressed");
startDial();
break;
default:
break;
}
}
/* This method is responsible for placing the phone call. */
public void startDial(){
//performs call
if (!phoneNumber.equals("")) {
Uri number = Uri.parse("tel:" + phoneNumber);
Intent dial = new Intent(Intent.ACTION_CALL, number);
Intent intent = new Intent(this, AlarmClock.class);
intent.setAction("com.example.allen.repeatdialer.CALL_PLACED");
sendBroadcast(intent);
startTime = System.currentTimeMillis();
startActivityForResult(dial, DIAL_COMMENCED);
}
}
}
Just to add some background information about this app, I had it working just fine using a handler, but the handler doesn't work if the CPU sleeps, so I am not trying to alter the application to use AlarmManager. So I know my issue lies within sending the broadcasts and receiving them.