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.