简体   繁体   中英

Android Notifications from AI in background

I'm building a prototype application of a larger system. This prototype will be offline but still look like it's getting information from a server. Even when the app is not open (using DeamonThread).

So I created the Android application and now trying to add an AI (within the app) that create and delete tasks. It works, but when I try to add Notifications from the DeamonThread it won't since Thread is not an Activity.

I tried to change it to extends Activity implements Runnable But then it's not possible to make it Deamon.

Feels like I'm missing something easy..

public void run() {
    while (counter < 100) {
        try {
            sleep(random.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Task task = new Task("AI", "this was the " + counter
                + " AI message", flow);
            sendNotation();
        }
        counter++;
    }
}

private void sendNotation() {
    NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, Flippin.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    String body = "This is a message from Adam";
    String title = "One new Task";
    NotificationCompat.Builder n = new NotificationCompat.Builder(this);
    n.setContentIntent(pi);
    n.setSmallIcon(R.drawable.notif);
    n.setContentTitle(title);
    n.setContentText(body);
    n.setDefaults(Notification.DEFAULT_ALL);
    n.setAutoCancel(true);
    nm.notify(uniqueID, n.build());
    finish();       
}

If you want to start a daemon, then you should look at Service

There are many tutorials out there for sending notification from Service.

And yes, it is possible to send notification from non-UI thread, using Handler .

This is the solution I was looking for, only thing I needed was to get access to the context (my application). NOTE I do believe this is a bad design, but since this is used only for a prototype I'll see it as perfect.

In Android Manifest file declare following

    <application android:name="com.example.MyApplication">

    </application>

then write the class

public class MyApplication extends Application{

private static Context context;

public void onCreate(){
    super.onCreate();
    MyApplication.context = getApplicationContext();
}

public static Context getAppContext() {
    return MyApplication.context;
}
}

Now every where call MyApplication.getAppContext() to get your application context statically.

private void sentNotation() {
    NotificationManager nm = (NotificationManager) MyApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(MyApplication.getAppContext(), Flippin.class);
    PendingIntent pi = PendingIntent.getActivity(MyApplication.getAppContext(), 0, intent, 0);
    String body = "This is a message from Adam";
    String title = "One new Task";
    NotificationCompat.Builder n = new NotificationCompat.Builder(MyApplication.getAppContext());
    n.setContentIntent(pi);
    n.setSmallIcon(R.drawable.notif);
    n.setContentTitle(title);
    n.setContentText(body);
    n.setDefaults(Notification.DEFAULT_ALL);
    n.setAutoCancel(true);
    nm.notify(uniqueId, n.build());
    finish();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM