When Alarm rang app open instead of dialog

2019-07-31 13:56发布

问题:

What I want to do

when alarm rang then a dialog box appear using AlertDialogActivity.class

Current status

Alarm is working perfectly but instead of calling the AlertDialogActivity.class MainActivity.class open(i.e. app opens).

Manifest File:

 <activity android:name="user.com.hlthee.MainActivity"
            android:theme="@style/AppTheme"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation|screenSize"
            />

        <activity android:name=".AlertDialogActivity"
            android:theme="@android:style/Theme.Dialog"
            android:launchMode="singleInstance"
            android:taskAffinity=""
            android:excludeFromRecents="true"
            android:screenOrientation="portrait"
            />

set alarm function

@SuppressLint("NewApi")
    private void setAlarm(String remCategory,Calendar calNow, Context context, int request_code) {

        init(context);
        Log.i("Inside setAlarm,","Yes");

        Log.i("setAlarm Category",remCategory);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("Rem_cat",remCategory);

        long alarmtime=calNow.getTimeInMillis();

        System.out.println("Reminder Time is "+calNow.get(Calendar.HOUR_OF_DAY)+" "+calNow.get(Calendar.MINUTE));
        Log.i("Target",Long.valueOf(alarmtime).toString());

        //final int _id = (int) System.currentTimeMillis();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            } else {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            }
        }
    }

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver {



       public static String TAG="Alarm Receiver";
        @Override
        public void onReceive(Context k1, Intent k2) {
            // TODO Auto-generated method stub

            Log.i("Alarm Received","Yes");

            String action = k2.getAction();

            String reminder_cat=k2.getExtras().getString("Rem_CAT");

            Calendar calendar=Calendar.getInstance();
            Intent i=new Intent(k1,AlertDialogActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("Rem_CAT",reminder_cat);

            Log.d(TAG,reminder_cat);
            k1.startActivity(i);
        }

    }

**AlertDialogActivity.class**

public class AlertDialogActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //hide activity title
        //setContentView(R.layout.notify);

        dbl = new DbHelper(AlertDialogActivity.this);

        Intent intent=getIntent();

        final String reminder_cat=intent.getStringExtra("Rem_CAT");

        Log.i("Reminder Catagory Alert",reminder_cat);

        final ArrayList<ListMedicine> alarm=dbl.fetchMedicineDetailsAccCatagory(reminder_cat);




        LayoutInflater li = LayoutInflater.from(this);
        //Creating a view to get the dialog box
        View confirmDialog = li.inflate(R.layout.custom_reminder, null);

    }

    //function to show the alert box even when screen is lock
    public void onAttachedToWindow() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    }

}