Hi
I wanted to run a piece of code in a background service so today I tried to test Services in AI2.
(this extension will be open source and free)
Here is the code:
//Start method
try {
context.startService(new Intent(context,BgService.class)
.putExtra("PROCEDURE_NAME",procedureName));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context,e.getMessage(),Toast.LENGTH_LONG);
}
//BgService class
public class BgService extends Service {
public Context mContext;
public NotificationManagerCompat notificationManager;
public BgService(){
super();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
notificationManager = NotificationManagerCompat.from(mContext);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,mContext.getApplicationInfo().name);
//builder.setContentTitle(title);
builder.setContentText("Service started by user");
builder.setAutoCancel(false);
builder.setVisibility(1);
builder.setPriority(1);
notificationManager.notify(12345,builder.build());
//Toast.makeText(context, "Service started by user.", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//Toast.makeText(context, "Service stopped by user.", Toast.LENGTH_LONG).show();
//notificationManager.cancel(12345);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,mContext.getApplicationInfo().name);
//builder.setContentTitle(title);
builder.setContentText("Service stopped by user");
builder.setAutoCancel(false);
builder.setVisibility(1);
builder.setPriority(1);
notificationManager.notify(12345,builder.build());
super.onDestroy();
}
}
//Uses Services annotation
@UsesServices(services = {@ServiceElement(exported = "false",name = "com.sunny.BackgroundTask.BackgoundTask$BgService")})
I have also tried with 1) a period, and 2)without package
Here is the error I am getting:
Unable to start service Intent { cmp=appinventor.ai_vknow360.CustomWebView/com.sunny.BackgroundTask.BackgroundTask$BgService } U=0: not found
Thank you