I am new to the Android API and I am trying to implement a scheduled process that reports back to the app.
So, I have the following code in my MainActivity
:
public class MainActivity extends AppCompatActivity {
private AlarmManager almgr;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter statusIntentFilter = new IntentFilter("Report");
BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Receiver","Received!");
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(
receiver, statusIntentFilter);
Intent mServiceIntent = new Intent(MainActivity.this, Background.class);
almgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getService(this,0,mServiceIntent,0);
almgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+1000, alarmIntent);
}
}
And this is my IntentService class
public class Background extends IntentService {
public Background(){super("Background");}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(this.toString(),"Service called");
Intent localIntent = new Intent("Report")
.putExtra("Value","True");
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
The IntentService
is receiving the initial Intent
and the Log entry in the Background class is printed but the BroadcastReceiver is not receiving the Report Intent
. It is not printing the "Receiver Received" Log.