Skip to content Skip to sidebar Skip to footer

Using Intentservice Instead Of Asynctask For Broadcast Receiver?

I am in the process of trying to get an email sent using BroadcastReceiver, the code is working correct using AsyncTask when using onClick but does not work when AlarmReceiver is b

Solution 1:

First start Intent service from Alarm manager :

privatevoidsetAlarm(Calendar targetCal){

   /* HERE */Intentintent=newIntent(getBaseContext(), AlarmService.class);
        finalint_id= (int) System.currentTimeMillis();

      /* HERE */PendingIntentpendingIntent= PendingIntent.getService(this,_id,intent,PendingIntent.FLAG_ONE_SHOT);

        AlarmManageralarmManager= (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    ......
    .....

Now Intent Service class:

publicclassAlarmServiceextendsIntentService {

PowerManager powerManager;
    PowerManager.WakeLock wakeLock;

publicAlarmService() {
        super("");
    }

    @OverrideprotectedvoidonHandleIntent(Intent intent) {
        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FCFCFCFC");

        wakeLock.acquire();

       addNotification();
       sendMAIL();

    }


    publicvoidaddNotification() {
    NotificationCompat.Builderbuilder=newNotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.icon_transperent)
                    .setLights(GREEN, 700, 700)
                    .setContentTitle("Achieve - Alert!")
                    .setContentText("This is a reminder for your deadline!");

    IntentnotificationIntent=newIntent(getApplicationContext(), MainMenu.class);
    PendingIntentcontentIntent= PendingIntent.getActivity(getApplicationContext(), 0,     notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notificationNotificationManagermanager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    builder.setVibrate(newlong[] { 0, 1000, 1000, 1000, 1000 });
    manager.notify(0, builder.build());
}

    publicvoidsendMAIL(){

    Mailm=newMail("youremail@gmail.com", "password");

        String[] toArr = {"toemail@outlook.com"};
        m.setTo(toArr);
        m.setFrom("fromemail@gmail.com");
        m.setSubject("Achieve Alert!");
        m.setBody("This is a reminder about your upcoming assignment or examination!");

        try {
            if(m.send()) {
                Toast.makeText(getApplicationContext(), "Email was sent successfully.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Email was not sent.", Toast.LENGTH_LONG).show();
            }
        } catch(Exception e) {
            Log.e("MailApp", "Could not send email", e);
        }


        wakeLock.release();

    }


     @OverridepublicvoidonDestroy() {
        super.onDestroy();
    }
}

Now, Manifest add:

<uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM"/><uses-permissionandroid:name="android.permission.WAKE_LOCK"/><serviceandroid:name=".AlarmService"android:exported="true"android:enabled="true"/>

Post a Comment for "Using Intentservice Instead Of Asynctask For Broadcast Receiver?"