简体   繁体   English

呼叫服务器功能和广播接收器android上的弹出窗口

[英]Call Server function and popup on Broadcast receiver android

I have an application in which I have to call off an alarm/notification each 30 Minutes. 我有一个应用程序,其中我必须每30分钟取消一次警报/通知。

I want the feature like 我想要这样的功能

1- If app is closed, it open the app, Call a dialog box. 1-如果关闭了应用程序,则会打开该应用程序,然后调用对话框。 On click it will call a serverFunction and if MainActivity is running, update its UI. 单击时,它将调用serverFunction,如果MainActivity正在运行,请更新其UI。

2- If the app is already opened , Call a dialog box. 2-如果该应用程序已经打开,请调用一个对话框。 On click it will call a serverFunction. 单击时将调用serverFunction。 Since MainActivity is may or may NOT on the top, update its UI Or NOT. 由于MainActivity可能位于或可能不在顶部,因此请更新其UI或不。

In My MainActivity.class 在我的MainActivity.class中

private void callNotification()
    {
        AlarmManager service = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


        Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);

        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());
        time.add(Calendar.SECOND, Constants.TIME_CONSTANT);

        service.set(AlarmManager.RTC_WAKEUP ,time.getTimeInMillis(), pending);        
    }



    public class AlarmReceiver extends BroadcastReceiver
    {
         @Override
         public void onReceive(final Context context, Intent intent) 
         {

              }
}

The problem here is , I can't put a dialog box in onReceive since context is not Activity context. 这里的问题是,我不能在onReceive中放置一个对话框,因为上下文不是Activity上下文。 What If the app is opened , Now how am I suppose to implement above features. 如果打开应用程序怎么办,现在我应该如何实现上述功能。

In your onReceive place this to call your activity: 在您的onReceive位置,这称为您的活动:

Intent i = new Intent(context, AlertActivity.class);
i.setFlags
startActivity(i);

Once you are in your activity you can open up a dialog. 进行活动后,您可以打开一个对话框。 I recommend you use a different activity than your main one to handle displaying the alert, as it makes sense from a design standpoint and it also makes implementation easier. 我建议您使用与主要活动不同的活动来处理警报的显示,因为从设计的角度来看这很有意义,并且也使实施更容易。 Remember you can make Activities look like dialogs... 记住,您可以使“活动”看起来像对话框...

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

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