首页 > 解决方案 > 当应用程序处于后台而不启动 mainActivity 时启动 Dialog 活动

问题描述

我有一些关于安卓的问题。我想启动一个活动,它是一种弹出活动(我在 AndroidManifest 中将此活动的主题设置为“Theme.Appcompat.Light.Dialog”)。所以我想做的就是当我从服务器收到一些推送消息时当应用程序在后台时,只显示此弹出活动。但是当我 startActivity() 时,整个应用程序从后台返回到前台并在 Mainactivity 上显示此弹出窗口。

所以,我不知道如何只显示弹出活动,而不是在 MainActivity 上。

请帮我。我只花了两天时间解决这个问题:(

我想发布有关此问题的照片,但由于缺乏声誉,我无法做到这一点。

下面是我的代码。谢谢!!

AndroidManifest.xml

<activity
        android:name=".activity.ActivityPopUpLocked"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.Dialog"
        android:launchMode="singleInstance"/>

onMessageReceived

 Context ctx=getApplicationContext();


    Intent itPop=new Intent(ctx, ActivityPopUpLocked.class);

    itPop.setAction(Intent.ACTION_NEW_OUTGOING_CALL);
    itPop.addCategory(Intent.CATEGORY_LAUNCHER);
    itPop.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    itPop.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    itPop.putExtra("content", strBody);
    itPop.putExtra("image", strImageUrl);

    //ctx.startActivity(itPop);

    PendingIntent pitPop = PendingIntent.getActivity(this,0,itPop,PendingIntent.FLAG_UPDATE_CURRENT);


    try {
        pitPop.send();
    } catch (PendingIntent.CanceledException e) {
        e.printStackTrace();
    }


    Intent it=new Intent(ctx, ActivityAlarm.class);
    it.setAction(Intent.ACTION_MAIN);
    it.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pit=PendingIntent.getActivity(this, 0, it, 0);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String strChannelId="1234";

    if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O) {

        NotificationChannel mChannel = notificationManager.getNotificationChannel(strChannelId);
        if (mChannel == null) {
            //IMPORTANCE_HIGH 여야 헤드업 알림 가능
            mChannel = new NotificationChannel(strChannelId, strTitle, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);
        }

        RemoteViews custom_layout = new RemoteViews(getPackageName(), R.layout.custom_notification);
        RemoteViews expanded_layout = new RemoteViews(getPackageName(), R.layout.custom_expanded);

        custom_layout.setImageViewBitmap(R.id.iv_push, myBitmap);
        custom_layout.setTextViewText(R.id.tv_push_title, strTitle);
        custom_layout.setTextViewText(R.id.tv_push_content, strBody);

        expanded_layout.setImageViewBitmap(R.id.iv_push, myBitmap);
        expanded_layout.setTextViewText(R.id.tv_push_title, strTitle);
        expanded_layout.setTextViewText(R.id.tv_push_content, strBody);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, strChannelId)
                        .setSmallIcon(R.drawable.push_icon)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setCustomContentView(custom_layout)
                        .setCustomBigContentView(expanded_layout)
                        .setContentTitle(strTitle) //헤드업 메시지 제목과 내용 표시
                        .setContentText(strBody)
                        .setContentIntent(pitPop) // 알림 클릭 시 이벤트
                        .setAutoCancel(true)
                        .setOngoing(false)
                        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)

                        //.setCategory(NotificationCompat.CATEGORY_CALL)  계속 폰콜처럼 떠있음
                        .setVisibility(Notification.VISIBILITY_PUBLIC)
                        /* bigpicture style
                        .setContentTitle(strTitle)
                        .setContentText(strBody)
                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(myBitmap))

                         */
                        //set background color
                        .setColor(getResources().getColor(R.color.white))
                        .setColorized(true)
                        //.setFullScreenIntent(pitPop, true)




                ;

        notificationManager.notify(nNotiId, notificationBuilder.build());

ActivityPopUpLocked.java

public class ActivityPopUpLocked extends BaseActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //PopUp의 Title을 제거
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_pop_up_locked);
    // 화면이 잠겨있을 때 보여주기

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED

            // 키잠금 해제하기

            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD

            // 화면 켜기

            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);



    TextView tvContent = (TextView)findViewById(R.id.tv_push_content);
    tvContent.setText(getIntent().getStringExtra("content"));
    ImageView ivPush = (ImageView)findViewById(R.id.iv_push_image);
    Glide.with(this).load(getIntent().getStringExtra("image")).into(ivPush);

    //ivPush.setImageBitmap(ServicePush.getImageFromURL(getIntent().getStringExtra("image")));
    Log.i("PopUpLocked", "image url = " + getIntent().getStringExtra("image"));


    Button btnNo = (Button)findViewById(R.id.btn_close);
    Button btnYes = (Button)findViewById(R.id.btn_check);



}

标签: javaandroid

解决方案


推荐阅读