attempt to use AlarmManager

This commit is contained in:
Alexander Mahr 2025-02-28 12:41:27 +01:00
parent 30c751bd0e
commit 46b16e8103
3 changed files with 150 additions and 88 deletions

View file

@ -175,6 +175,8 @@ box-shadow: 0 0 10px grey;
<div id="settingsDiv">
<button style="padding:1cm;font-size:1cm;margin:1cm;" onclick='window.counti=window.counti|| 1; window.counti++;preout.textContent+="\n"+myJavaScriptInterface.androidNotification("message"+Date.now(),"title",window.counti);'>test notification</button>
<pre id="preout"></pre>
<button style="padding:1cm;font-size:1cm;margin:1cm;" onclick='window.counti=window.counti|| 1; window.counti++;alarmout.textContent+="\n"+myJavaScriptInterface.setAlarm();'>test alarm</button>
<pre id="alarmout"></pre>
</div>
<
</div>

View file

@ -18,102 +18,119 @@ import java.util.Objects;
import android.app.Notification;
import android.app.NotificationManager;
import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
//import android.graphics.Bitmap;
//import android.graphics.drawable.Icon;
public class AppActivity extends Activity {
private Icon icon;
private Bitmap bitmap;
// private Icon icon;
// private Bitmap bitmap;
private static final String BASE_URI = "https://alexmahr.de";
public String readFileFromAssets(String filename) {
String filecontents = "";
try {
InputStream stream = getAssets().open(filename);
int filesize = stream.available();
byte[] filebuffer = new byte[filesize];
stream.read(filebuffer);
stream.close();
filecontents = new String(filebuffer);
} catch (Exception e) {
// I <3 java exceptions
}
return filecontents;
private static final String BASE_URI = "https://alexmahr.de";
public String readFileFromAssets(String filename) {
String filecontents = "";
try {
InputStream stream = getAssets().open(filename);
int filesize = stream.available();
byte[] filebuffer = new byte[filesize];
stream.read(filebuffer);
stream.close();
filecontents = new String(filebuffer);
} catch (Exception e) {
// I <3 java exceptions
}
return filecontents;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
// determin why the checking of this did not work
// int PERMISSION_GRANTED=0;
// if( checkSelfPermission( Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED)
// {
try {
requestPermissions( new String[] {Manifest.permission.POST_NOTIFICATIONS},1);
} catch (Exception e) { } ;
WebView myWebView = new WebView(this);//activityContext);
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
// WebViewClient myWebViewClient= new WebViewClient();
// myWebView.setWebViewClient(myWebViewClient);
WebChromeClient myWebChromeClient= new WebChromeClient();
myWebView.setWebChromeClient(myWebChromeClient);
setupNotificationChannelShit();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
// determin why the checking of this did not work
// int PERMISSION_GRANTED=0;
// if( checkSelfPermission( Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED)
// {
try {
requestPermissions( new String[] {Manifest.permission.POST_NOTIFICATIONS},1);
} catch (Exception e) { } ;
WebView myWebView = new WebView(this);//activityContext);
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
// WebViewClient myWebViewClient= new WebViewClient();
// myWebView.setWebViewClient(myWebViewClient);
WebChromeClient myWebChromeClient= new WebChromeClient();
myWebView.setWebChromeClient(myWebChromeClient);
setupNotificationChannelShit();
// to setup settings
WebSettings myWebSettings = myWebView.getSettings();
// enable/disable debugging
myWebView.setWebContentsDebuggingEnabled(true);
myWebSettings.setBuiltInZoomControls(true);
myWebSettings.setDisplayZoomControls(false);
myWebSettings.setJavaScriptEnabled(true);
myWebSettings.setDomStorageEnabled(true);
myWebSettings.setDatabaseEnabled(true);
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
// load the html from assets file
String html = readFileFromAssets("index.html");
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
//myWebView.loadData(encodedHtml, "text/html", "base64");
// alternatively this could be to load a website
//myWebView.loadUrl("https://alexmahr.de/ru");
setContentView(myWebView);
}
public void setupNotificationChannelShit(){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, "calender app channel",importance);
mChannel.setDescription("description");
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
// to setup settings
WebSettings myWebSettings = myWebView.getSettings();
// enable/disable debugging
myWebView.setWebContentsDebuggingEnabled(true);
myWebSettings.setBuiltInZoomControls(true);
myWebSettings.setDisplayZoomControls(false);
myWebSettings.setJavaScriptEnabled(true);
myWebSettings.setDomStorageEnabled(true);
myWebSettings.setDatabaseEnabled(true);
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
// load the html from assets file
String html = readFileFromAssets("index.html");
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
//myWebView.loadData(encodedHtml, "text/html", "base64");
// alternatively this could be to load a website
//myWebView.loadUrl("https://alexmahr.de/ru");
setContentView(myWebView);
}
@JavascriptInterface
public String androidNotification(String message,String title,int id) {
Notification notif = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(getResources().getIdentifier("appicon", "drawable", getPackageName()))
.setChannelId("my_channel_01")
.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(id, notif);
return message+ title+"this is good";
}
@JavascriptInterface
public String toString() {
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
return "this is good";
}
@JavascriptInterface
public String setAlarm(){
Intent myIntent = new Intent(getBaseContext(), MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, PendingIntent.FLAG_MUTABLE );
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
long interval = 10 * 1000; //
alarmManager.setExact(AlarmManager.RTC_WAKEUP,system.currentTimeMillis()+10000 , pendingIntent);
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), interval, pendingIntent);
return "alarm set";
}
public void setupNotificationChannelShit(){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, "calender app channel",importance);
mChannel.setDescription("description");
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
@JavascriptInterface
public String androidNotification(String message,String title,int id) {
Notification notif = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(getResources().getIdentifier("appicon", "drawable", getPackageName()))
.setChannelId("my_channel_01")
.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(id, notif);
return message+ title+"this is good";
}
@JavascriptInterface
public String toString() {
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
return "this is good";
}
}

View file

@ -0,0 +1,43 @@
package calender.app;
import android.provider.Settings ;
import android.content.Intent;
import android.util.Log;
import android.app.Activity;
import android.app.NotificationChannel;
import android.os.Bundle;
import android.os.Environment;
//import android.view.MenuItem;
import android.view.*;
// for WebView,WebMessage,WebMessagePort,
import android.webkit.*;
import android.net.Uri;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.File;
import java.util.Objects;
import android.app.Notification;
import android.app.NotificationManager;
import android.Manifest;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.util.Log;
//import android.graphics.Bitmap;
//import android.graphics.drawable.Icon;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("myapp","onReceive CALLED");
Intent myIntent =new Intent(context, AppActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
System.out.println("Receiver Call");
}
}