-1

i'm in a project to schedule a task with an calendar and do the job in the specific hour, the hole code is alright, working, but if i force close my app in android in the specific hour i puted in the begin of the aplication don't do my task it give me a "unfortunately TransferirLigações has stopped". i think the timer and the AlarmManager is working, but don't do my task, give a error.

I tried ALL kinds of Scheduling task to do this, AlarmManager and Receiver, Service, IntentService, Job Scheduler and some plugins to do this, but nothing work, all give me "unfortunately TransferirLigações has stopped".

Anyone can help me? I thinks it is some errors in Manifest or Permisions, but i don't know.

Codes:

MainActivity, startAlarm class:

 private void startAlarm() {
            Intent intent = new Intent(getApplicationContext(), AlarmToastReceiver.class);
            final PendingIntent pIntent = PendingIntent.getBroadcast(this, AlarmToastReceiver.REQUEST_CODE,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
            alarm.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);

        }

Receiver Class: package com.example.usuario.tranferircalls;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class AlarmToastReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;


    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyTestService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}

IntentService:

package com.example.usuario.tranferircalls;

import android.app.IntentService;

import android.content.Intent;

import java.util.concurrent.TimeUnit;

import static com.example.usuario.tranferircalls.MainActivity.cancelAlarm;
import static com.example.usuario.tranferircalls.MainActivity.retornarChamada;


public class MyTestService extends IntentService {
    public MyTestService() {
        // Used to name the worker thread
        // Important only for debugging
        super(MyTestService.class.getName());
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        retornarChamada(0); //My Task, that i want to do when certain time pass.
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        retornarChamada(1);
        cancelAlarm();
    }
}

MyManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.usuario.tranferircalls">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:installLocation="internalOnly"
        android:theme="@style/AppTheme">
        <activity
            android:name=".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>
        <receiver
            android:name=".AlarmToastReceiver"
            android:exported="true"
            android:process=":remote"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <service
            android:name=".MyTestService"
            android:enabled="true"
            android:exported="false"/>
    </application>
</manifest>

Basically, thats is my app, it is 90% working, but i want to close app and it work, for have a certain that my Job / Task will work well.

grateful already, Noninus.

Rafael Nonino
  • 180
  • 4
  • 15

3 Answers3

0

Try this:

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    am.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else
    am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
Pang
  • 9,564
  • 146
  • 81
  • 122
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • i tried remove this code from Manifest, but don't work, don't changed nothing, when app is opened works well, but when the app is closed give me an error message "unfortunately TransferirLigações has stopped". – Rafael Nonino Mar 27 '18 at 14:40
  • If this is not working, please put your time calculation code. `cal.getTimeInMillis()`. paste your code how you calculated cal. – Shalu T D Mar 27 '18 at 14:51
  • don't work too, do you thinks is the error on manifest? so much permissions? or ? @Shalu T D – Rafael Nonino Mar 27 '18 at 14:52
  • "unfortunately TransferirLigações has stopped" with this codes and this new AlarmManager, but thanks for trying helping! The code work well when app is opened, but when i force close it it don't work – Rafael Nonino Mar 27 '18 at 15:00
  • Use `Calendar.getInstance().getTimeInMillis() + 5min` replace `cal.getTimeInMillis()` – Shalu T D Mar 27 '18 at 15:00
  • i tried too, and don't work too, i don't think the problem is on Calendar or Timers, because the action works in the certain time every time, but if the application is closed the action works and the alright time, but give-me the error message, in the right time. – Rafael Nonino Mar 27 '18 at 19:46
0

The Code for calculate time, it is a TimePicker that get the time, set in a static Calendar cal, and i use it to start the AlarmManager!

Calendar mcurrentTime;
                    mcurrentTime = Calendar.getInstance();
                    mcurrentTime.setTimeInMillis(System.currentTimeMillis());


                    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                    int minute = mcurrentTime.get(Calendar.MINUTE);

                    TimePickerDialog mTimePicker;
                    mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {


                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {           
                            cal.set(Calendar.HOUR_OF_DAY,selectedHour);
                            cal.set(Calendar.MINUTE,selectedMinute);
                            endTime = cal.getTime();
                        }
                    }, hour, minute, true);//Yes 24 hour time

                    mTimePicker.setTitle("Escolha a Hora para as chamadas voltarem!");
                    mTimePicker.show();
Rafael Nonino
  • 180
  • 4
  • 15
0

I would try that:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class AlarmToastReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;


    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyTestService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}