I have referred from this link and tried this code So far.But I don't know to implement this code in gluon-mobile.What I intent to do is that I want to start an Indent that does some Job in background at a specific time.My problem is that I don't how gluon-mobile behaves.So help me complete it.
package com.application;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import javafxports.android.FXActivity;
public class Schedular {
AlarmManager manager=null;
FXActivity activity=null;
public Schedular()
{
manager=(AlarmManager) FXActivity.getInstance().getSystemService(Context.ALARM_SERVICE);
activity=FXActivity.getInstance();
}
public void schedule()
{
Intent indent = new Intent(activity,Alarm.class);
PendingIntent pIntent = PendingIntent.getBroadcast(activity, 0, indent, 0);
manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+2*1000,indent); //This is Where I went Wrong
}
}
I want to schedule a task at specific timing given by the user at the Run-time,That's what the function public void schedule(Calendar calendar,boolean flag) does.
Alarm.class
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Your code to execute when the alarm triggers
// and the broadcast is received.
new bluetooth.turnOn();
}
And I have added Alarm.class to AndroidManifest.xml as
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.application" android:versionCode="1" android:versionName="1.0">
<supports-screens android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="26"/>
<application android:label="Wifischeduler" android:name="android.support.multidex.MultiDexApplication" android:icon="@mipmap/ic_launcher">
<activity android:name="javafxports.android.FXActivity" android:label="Wifischeduler" android:configChanges="orientation|screenSize">
<meta-data android:name="main.class" android:value="com.application.Main"/>
<meta-data android:name="debug.port" android:value="0"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.gluonhq.impl.charm.down.plugins.android.NotificationActivity"
android:parentActivityName="javafxports.android.FXActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="javafxports.android.FXActivity"/>
</activity>
<activity android:name="com.application.AndroidPlatform$PermissionRequestActivity" />
<receiver android:name="com.application.Alarm" /> // this is where I have included the Alarm.class
<receiver android:name="com.gluonhq.impl.charm.down.plugins.android.AlarmReceiver" />
</application>
When the schedule method is called the app exits...
I have run adb logcat -v threadtime and got this output on cmd
--------- beginning of crash
07-22 09:21:44.878 13516 13516 E AndroidRuntime: FATAL EXCEPTION: main
07-22 09:21:44.878 13516 13516 E AndroidRuntime: Process: com.application, PID: 13516
07-22 09:21:44.878 13516 13516 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate receiver com.application.Alarm: java.lang.IllegalAccessException: java.lang.Class<com.application.Alarm> is not accessible from java.lang.Class<android.app.ActivityThread>
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2739)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.app.ActivityThread.access$1900(ActivityThread.java:153)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5529)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: Caused by: java.lang.IllegalAccessException: java.lang.Class<com.application.Alarm> is not accessible from java.lang.Class<android.app.ActivityThread>
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at java.lang.Class.newInstance(Native Method)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2734)
07-22 09:21:44.878 13516 13516 E AndroidRuntime: ... 8 more
07-22 09:21:44.885 1804 3742 D StatusBar: onNotificationPosted: Key: 0|com.application|123456|charm://down/Id/abcd12340|10276 GroupKey: 0|com.application|123456|charm://down/Id/abcd12340|10276 Connected: true
07-22 09:21:44.885 1804 1804 D StatusBar: GroupChild: false GroupsContainsKey: false IsUpdate: false IsGroupSummary: false hasIcon: true
07-22 09:21:44.886 1804 1804 D PhoneStatusBar: addNotification pkg=com.application;basepkg=com.application;id=123456
07-22 09:21:44.888 3047 10359 I GCoreUlr: Successfully inserted 1 locations
07-22 09:21:44.922 1804 1804 W ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@eb9196
07-22 09:21:44.922 1804 1804 W ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@aace117
07-22 09:21:44.932 3451 3518 I octvm_klo: klo lock
I have no idea why I can't Instantiate a Receiver (Alarm.class),
@FXML void start()
{
try{
Schedule schedule =(Schedule) Class.forName("com.application.Schedular").newInstance();
schedule.schedule();
}