attempt to use AlarmManager
This commit is contained in:
parent
30c751bd0e
commit
46b16e8103
3 changed files with 150 additions and 88 deletions
|
@ -175,6 +175,8 @@ box-shadow: 0 0 10px grey;
|
||||||
<div id="settingsDiv">
|
<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>
|
<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>
|
<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>
|
||||||
<
|
<
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,102 +18,119 @@ import java.util.Objects;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.graphics.Bitmap;
|
import java.util.Calendar;
|
||||||
import android.graphics.Color;
|
import android.app.AlarmManager;
|
||||||
import android.graphics.drawable.Icon;
|
import android.app.PendingIntent;
|
||||||
|
//import android.graphics.Bitmap;
|
||||||
|
//import android.graphics.drawable.Icon;
|
||||||
|
|
||||||
public class AppActivity extends Activity {
|
public class AppActivity extends Activity {
|
||||||
|
|
||||||
private Icon icon;
|
// private Icon icon;
|
||||||
private Bitmap bitmap;
|
// private Bitmap bitmap;
|
||||||
|
|
||||||
private static final String BASE_URI = "https://alexmahr.de";
|
|
||||||
public String readFileFromAssets(String filename) {
|
private static final String BASE_URI = "https://alexmahr.de";
|
||||||
String filecontents = "";
|
public String readFileFromAssets(String filename) {
|
||||||
try {
|
String filecontents = "";
|
||||||
InputStream stream = getAssets().open(filename);
|
try {
|
||||||
int filesize = stream.available();
|
InputStream stream = getAssets().open(filename);
|
||||||
byte[] filebuffer = new byte[filesize];
|
int filesize = stream.available();
|
||||||
stream.read(filebuffer);
|
byte[] filebuffer = new byte[filesize];
|
||||||
stream.close();
|
stream.read(filebuffer);
|
||||||
filecontents = new String(filebuffer);
|
stream.close();
|
||||||
} catch (Exception e) {
|
filecontents = new String(filebuffer);
|
||||||
// I <3 java exceptions
|
} catch (Exception e) {
|
||||||
}
|
// I <3 java exceptions
|
||||||
return filecontents;
|
|
||||||
}
|
}
|
||||||
|
return filecontents;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
|
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
|
||||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
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....)
|
// 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
|
// determin why the checking of this did not work
|
||||||
// int PERMISSION_GRANTED=0;
|
// int PERMISSION_GRANTED=0;
|
||||||
// if( checkSelfPermission( Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED)
|
// if( checkSelfPermission( Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED)
|
||||||
// {
|
// {
|
||||||
try {
|
try {
|
||||||
requestPermissions( new String[] {Manifest.permission.POST_NOTIFICATIONS},1);
|
requestPermissions( new String[] {Manifest.permission.POST_NOTIFICATIONS},1);
|
||||||
} catch (Exception e) { } ;
|
} catch (Exception e) { } ;
|
||||||
WebView myWebView = new WebView(this);//activityContext);
|
WebView myWebView = new WebView(this);//activityContext);
|
||||||
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
|
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
|
||||||
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
|
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
|
||||||
// WebViewClient myWebViewClient= new WebViewClient();
|
// WebViewClient myWebViewClient= new WebViewClient();
|
||||||
// myWebView.setWebViewClient(myWebViewClient);
|
// myWebView.setWebViewClient(myWebViewClient);
|
||||||
WebChromeClient myWebChromeClient= new WebChromeClient();
|
WebChromeClient myWebChromeClient= new WebChromeClient();
|
||||||
myWebView.setWebChromeClient(myWebChromeClient);
|
myWebView.setWebChromeClient(myWebChromeClient);
|
||||||
|
|
||||||
setupNotificationChannelShit();
|
setupNotificationChannelShit();
|
||||||
|
|
||||||
|
|
||||||
// to setup settings
|
// to setup settings
|
||||||
WebSettings myWebSettings = myWebView.getSettings();
|
WebSettings myWebSettings = myWebView.getSettings();
|
||||||
// enable/disable debugging
|
// enable/disable debugging
|
||||||
myWebView.setWebContentsDebuggingEnabled(true);
|
myWebView.setWebContentsDebuggingEnabled(true);
|
||||||
myWebSettings.setBuiltInZoomControls(true);
|
myWebSettings.setBuiltInZoomControls(true);
|
||||||
myWebSettings.setDisplayZoomControls(false);
|
myWebSettings.setDisplayZoomControls(false);
|
||||||
myWebSettings.setJavaScriptEnabled(true);
|
myWebSettings.setJavaScriptEnabled(true);
|
||||||
myWebSettings.setDomStorageEnabled(true);
|
myWebSettings.setDomStorageEnabled(true);
|
||||||
myWebSettings.setDatabaseEnabled(true);
|
myWebSettings.setDatabaseEnabled(true);
|
||||||
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
|
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
|
||||||
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
||||||
// load the html from assets file
|
// load the html from assets file
|
||||||
String html = readFileFromAssets("index.html");
|
String html = readFileFromAssets("index.html");
|
||||||
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
|
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
|
||||||
//myWebView.loadData(encodedHtml, "text/html", "base64");
|
//myWebView.loadData(encodedHtml, "text/html", "base64");
|
||||||
// alternatively this could be to load a website
|
// alternatively this could be to load a website
|
||||||
//myWebView.loadUrl("https://alexmahr.de/ru");
|
//myWebView.loadUrl("https://alexmahr.de/ru");
|
||||||
setContentView(myWebView);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public String androidNotification(String message,String title,int id) {
|
public String setAlarm(){
|
||||||
Notification notif = new Notification.Builder(this)
|
Intent myIntent = new Intent(getBaseContext(), MyReceiver.class);
|
||||||
.setContentTitle(title)
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, PendingIntent.FLAG_MUTABLE );
|
||||||
.setContentText(message)
|
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
|
||||||
.setSmallIcon(getResources().getIdentifier("appicon", "drawable", getPackageName()))
|
Calendar calendar = Calendar.getInstance();
|
||||||
.setChannelId("my_channel_01")
|
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||||
.build();
|
calendar.add(Calendar.SECOND, 5);
|
||||||
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
long interval = 10 * 1000; //
|
||||||
nm.notify(id, notif);
|
alarmManager.setExact(AlarmManager.RTC_WAKEUP,system.currentTimeMillis()+10000 , pendingIntent);
|
||||||
return message+ title+"this is good";
|
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), interval, pendingIntent);
|
||||||
}
|
return "alarm set";
|
||||||
@JavascriptInterface
|
}
|
||||||
public String toString() {
|
|
||||||
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
|
public void setupNotificationChannelShit(){
|
||||||
return "this is good";
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
43
app/src/calender/app/MyReceiver.java
Normal file
43
app/src/calender/app/MyReceiver.java
Normal 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue