简体   繁体   English

从广播接收器android调用活动警报框

[英]Call an activity Alert Box from Broadcast Receiver android

I have an app which launches a Broadcast. 我有一个启动广播的应用程序。 I want to display a popup no matter on which activity i am if the app is running and if not then push a notification 如果应用程序正在运行,无论我正在进行什么活动,我都想显示一个弹出窗口,如果没有,则推送通知

Here is my code 这是我的代码

public class AlarmReceiver extends BroadcastReceiver
{
    private static int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;

     @Override
     public void onReceive(final Context ctx, Intent intent) 
     {
         if (isRunning(ctx))
         {
            Intent myIntent = new Intent (ctx, NotifyTagabilityCounter.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctx.startActivity(myIntent);
         }
         else
         {
             mNotificationManager = (NotificationManager)
                      ctx.getSystemService(Context.NOTIFICATION_SERVICE);

             Intent i = new Intent(ctx, Game.class);
             i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             i.putExtra(Constants.TAG_TIMESTAMP, true);
             PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                  i, PendingIntent.FLAG_CANCEL_CURRENT);      

             NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(ctx)
                  .setSmallIcon(R.drawable.launcher)

             Notification notification = mBuilder.getNotification();
             notification.flags = Notification.FLAG_AUTO_CANCEL;

             mBuilder.setContentIntent(contentIntent);
             mNotificationManager.notify(NOTIFICATION_ID, notification);

             SharedPreferences customSharedPreference = ctx.getSharedPreferences(
                     "UserSharedPrefs", Activity.MODE_PRIVATE);

            SharedPreferences.Editor editor = customSharedPreference.edit();
            editor.putBoolean(Constants.TAG_NOTIFICATION, true);
            editor.commit();
         }
     }

     public boolean isRunning(Context ctx) 
     {
            ActivityManager activityManager = (ActivityManager)  ctx.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> tasks =  activityManager.getRunningTasks(Integer.MAX_VALUE);

            for (RunningTaskInfo task : tasks) 
            {
                if (ctx.getPackageName().equalsIgnoreCase(
                                         task.baseActivity.getPackageName())) 
                    return true;                                  
            }

            return false;
        }

On creating new Activity NotificyTagabilityCounter 关于创建新的Activity NotificyTagabilityCounter

ublic class NotifyTagabilityCounter extends Activity
{
    public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
    static final String ACTION = "com.trib.broadcast";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(ACTION);
        this.registerReceiver(mReceivedReceiver, filter);
    }

private final BroadcastReceiver mReceivedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (ACTION.equals(action)) 
            {
                displayAlert();
            }
        }
    };    

All I get is a black screen. 我得到的只是一个黑屏。 I want my last activity to be shown and on top of it a popup AlertDisplay is shown. 我希望显示我的上一个活动,并在其顶部显示一个弹出警报显示。 How can this be done..? 如何才能做到这一点..?

To done that: 为此:
1- define two BroadcastReceivers the 1st one in the manifest with low priority and the 2nd one in each activity in your app (run time) with a higher priority. 1-定义两个BroadcastReceivers ,第一个在清单中具有较低的优先级,第二个在应用程序中每个活动(运行时)中具有较高的优先级。
The 1st BroadcastReceivers should show a notification 第一BroadcastReceivers应显示一条通知
and the 2nd broadcast should abort() the broadcast and display the dialog. 并且第二广播应该abort()广播,并显示对话框。

2- send an ordered broadcast. 2-发送有序的广播。

PS: you can create a BaseActivtiy witch extends Activity, and define the 2nd broadcast on it (dont foget to register and unregister the broadcast in onStart() and onStop() ), PS:您可以创建一个扩展Activity的BaseActivtiy女巫,并在其上定义第二个广播(不要在onStart()onStop()注册和注销广播),
then make all your Activity`s extends the BaseActivtiy 然后让您所有的Activity扩展BaseActivtiy

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

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