I built an android app that gets a message from a server and pass it to the notification … every thing work wonderful … when i close the app (shutdown) … i stop getting the notification …
how can i make my app working at the background like “whatsApp” when it closed she still getting messages
i read and try sketch’s from the net … but no success …
Ok… I missed a couple of important things. Firs of all, the service class needs to be declared in the manifest file… and also think the source file for DummyService should be a .java instead of .pde, so…
FATAL EXCEPTION: main
Process: processing.test.service_demo, PID: 2514
java.lang.RuntimeException: Unable to instantiate service processing.test.service_demo.DummyService: java.lang.IllegalAccessException: java.lang.Class<processing.test.service_demo.DummyService> is not accessible from java.lang.Class<android.app.ActivityThread>
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3446)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.IllegalAccessException: java.lang.Class<processing.test.service_demo.DummyService> is not accessible from java.lang.Class<android.app.ActivityThread>
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3443)
… 8 more
I insert whats inside setup() to my setup() program .
and add the functions below draw() to my sketch …
my sketch name is “AnnexSafe” … which reports building worker accidents or hazard from the field to an office server (excel sheet) … and receive notification from the server If the issue has not been addressed .
so , i didnt access my App when i tap the notification … i try some codes from internet … no access
Hi, the tricky part of using services is that the code in the java file does not know anything about the sketch, you need to explicitly send a reference to the sketch object so the service can access all your functions. A not-very elegant way to do this, but which seems to work, is as follows (only listing the changes):
hi,
thanx …
i have this Error !
working …
FATAL EXCEPTION: main
Process: processing.test.annexsafe, PID: 9475
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getByName(InetAddress.java:708)
at processing.test.annexsafe.AnnexSafe.isConnectingToInternet(AnnexSafe.java:2288)
at processing.test.annexsafe.DummyService$1.run(DummyService.java:91)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
I still having the problem when the App goes to the background (when i close all the applications) the loop function “void workLoop()” stopped of working (i think there as an error witch stopped the loop function)
Here is my function i used at processing sketch :
public void isConnectingToInternet(){
ConnectivityManager connectivityManager = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
if( activeNetworkInfo.isConnected()) {
try{
address = InetAddress.getByName("www.google.com");
String hostIP = address.getHostAddress() ;
if(hostIP.equals(currentIP) == false ) {
ConnectStatus = "disConnected" ;
currentIP = hostIP ;
}
if (ConnectStatus.equals("disConnected")) {
MyConnection();
}
} catch(UnknownHostException e ){ ConnectStatus = "disConnected" ;}
} else { ConnectStatus = "disConnected" ; }
}else { ConnectStatus = "disConnected" ; }
}
public void MyConnection()
{
client = new MuKCastClient(this, ServerIP, int(ServerPort));
try {
client.connect();
ConnectStatus = "Connected";
} catch(IOException e) {
// e.printStackTrace();
ConnectStatus = "disConnected";
}
}
here the code i used in java file : mu pde file called AnnexSafe
import android.app.Activity;
import android.app.Service;
import android.os.Handler;
import android.os.IBinder;
import android.content.Intent;
import android.app.Notification;
import android.app.PendingIntent;
import processing.core.PApplet;
import android.os.StrictMode;
public class DummyService extends Service {
public AnnexSafe sketch;
static final String TAG = "DummyService";
static final int FOREGROUND_NOTIFICATION_ID = 1;
static Activity activity;
static DummyService instance;
boolean running = false;
Handler handler = new Handler();
Runnable runnable;
int WAIT_SECONDS = 5;
// public DummyService(PApplet sketch) {
// this.sketch = sketch;
//}
@Override
public void onCreate() {
System.out.println("Created service");
super.onCreate();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
start();
instance = this;
}
@Override
public void onDestroy() {
stop();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// Used only in case of bound services.
return null;
}
void start() {
goForeground();
workLoop();
running = true;
}
void stop() {
running = false;
}
void goForeground() {
Intent notificationIntent = new Intent(this, activity.getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification m = new Notification.Builder(this)
.setContentTitle("Dummy Service")
.setContentText("Testing foreground services from Processing.")
.setContentIntent(pendingIntent)
.build();
startForeground(FOREGROUND_NOTIFICATION_ID, m);
}
void setSketch(AnnexSafe sketch) {
this.sketch = sketch;
}
void workLoop() {
System.out.println("launching ....");
// runnable = new Runnable() {
Thread thread = new Thread(runnable = new Runnable(){
@Override
public void run() {
System.out.println("working ....");
if (sketch != null) {
System.out.println("hi ....");
sketch.isConnectingToInternet();
}
if (running) {
handler.postDelayed(runnable, WAIT_SECONDS * 1000);
}
}
});
thread.start();
handler.postDelayed(runnable, WAIT_SECONDS * 1000);
}
}