lofcian
lofcian

Reputation: 35

open layout without touching notification in Android

i made an alarm clock in android. I want when alarm time comes, programmatically open a layout without click notification.

Here's the codes;

that's my fireAlarm function

public void fireAlarm() {
c = Calendar.getInstance(); 
seconds = c.get(Calendar.SECOND)*1000;
minutes = c.get(Calendar.MINUTE)*60*1000;
hours = c.get(Calendar.HOUR)*3600*1000;
ct = seconds + minutes + hours;
sure = (tp.getCurrentMinute()*60*1000) + (tp.getCurrentHour()*3600*1000);
finalarm = sure - ct;
Toast.makeText(MainActivity.this, "Alarm " + finalarm/1000 + " Saniye sonra çalacak", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.set( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+finalarm , pendingIntent );
}

and that's notification codes

private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
notificationManager = (NotificationManager) context
      .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 111,notificationIntent, 0);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        Intent.FLAG_ACTIVITY_SINGLE_TOP);

    notification.setLatestEventInfo(context, title, subTitle, intent);
    //To play the default sound with your notification:
    notification.sound = Uri.parse(MainActivity.path);
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

should i use different service or something? thank you.

Upvotes: 1

Views: 138

Answers (1)

Ringo
Ringo

Reputation: 838

In case you are using a broadcast receiver, your "onReceive" overridden method should have a Context received as an argument, which is the context of the activity that sends the alarm, in that case simply:

notificationIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(notificationintent); 

somewhere in your generateNotification method.

If you are not using a broadcast receiver, it may be a better approach.

Or simply put this code at the end of your generateNotification method, it may actually work, just haven't tested my self.

Upvotes: 2

Related Questions