简体   繁体   English

后台来自AI的Android通知

[英]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). 即使未打开应用程序(使用DeamonThread)。

So I created the Android application and now trying to add an AI (within the app) that create and delete tasks. 因此,我创建了Android应用程序,现在尝试添加一个AI(在应用程序内)以创建和删除任务。 It works, but when I try to add Notifications from the DeamonThread it won't since Thread is not an Activity. 它可以正常工作,但是当我尝试从DeamonThread添加通知时,由于Thread不是Activity,所以它不会。

I tried to change it to extends Activity implements Runnable But then it's not possible to make it Deamon. 我试图将其更改为扩展Activity实现Runnable,但是不可能将其设为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 如果要启动守护程序,则应查看Service

There are many tutorials out there for sending notification from Service. 有很多教程可以从Service发送通知。

And yes, it is possible to send notification from non-UI thread, using Handler . 是的,可以使用Handler从非UI线程发送通知。

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 在Android Manifest文件中声明以下内容

    <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. 现在,每个地方都调用MyApplication.getAppContext()以静态获取您的应用程序上下文。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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