0

I have application and I need to open dialog at a specific time, I searched on the internet but it's not working, for example, I need to open dialog every day at 8:00 but how I can handle when the app is closed ???

this is something I found on the internet

   AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(),
            2*60*1000,
            pendingIntent);
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Moayed Alayseh
  • 188
  • 3
  • 14

2 Answers2

0

try above codes it may help using Notification

MainActivity.java

      package com.example;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;


public class MainActivity extends Activity 
{

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = Calendar.getInstance();


        //set notification for date --> 8th January 2015 at 9:06:00 PM , here month starts from 0 like 0 to Jan, 1 to Feb...
        calendar.set(Calendar.MONTH, 6);
        calendar.set(Calendar.YEAR, 2017);
        calendar.set(Calendar.DAY_OF_MONTH, 18);

        calendar.set(Calendar.HOUR_OF_DAY, 18);
        calendar.set(Calendar.MINUTE, 22);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.AM_PM,Calendar.PM);

        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    } //end onCreate

}

MyReciever.java

     package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        /*Intent service1 = new Intent(context, MyAlarmService.class);
         context.startService(service1);*/
        Log.i("App", "called receiver method");
        try{
            Utils.generateNotification(context);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

now finally create utils.java

       package com.example;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Utils {

    public static NotificationManager mManager;

    @SuppressWarnings("static-access")
    public static void generateNotification(Context context){ 

     Notification.Builder builder = new Notification.Builder(context);
    Intent notificationIntent = new Intent(context,MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
    builder.setSmallIcon(R.drawable. notification_template_icon_bg)
            .setContentTitle("This is a test message!")
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    Notification notification = builder.getNotification();
    notificationManager.notify(R.drawable.notification_template_icon_bg, notification);
    }
}

your manifest file as like above

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />


    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyReceiver"/>


    </application>

</manifest>

for more checkout this link of github repo

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • sry but this line ,,, notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); setLatestEVent not reslove !!! – Moayed Alayseh Jul 18 '17 at 12:40
  • this setLatestEventInfo not Resolve and without it the app is not running – Moayed Alayseh Jul 18 '17 at 13:09
  • @MoayedAlayseh setLatestEventInfo() is depricated in API 23 now try updated util.java file – Omkar Jul 18 '17 at 13:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149490/discussion-between-omi-and-moayed-alayseh). – Omkar Jul 18 '17 at 13:36
0

I recommend that you don't do this (it goes against Android design and UI guidelines). Notifications are the preferred way to accomplish what you are doing

If you really want to show dialog I would recommend just using a Dialog themed activity. That way you don't have to start up a separate dialog. Create Activity as Dialog Theme and start that Activity from Service.

1) Create your style for your dialog activity

<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:background">@android:color/transparent</item>
        </style>

2) Register you Activity in manifest.xml

            <activity
            android:name=".activity.NotificationActivity"
            android:theme="@style/Theme.CustomTranslucent">
yashkal
  • 713
  • 5
  • 20
  • You can get callback of your alarm right ?, when you get callback you have to give this callback to your broadcast receiver(which is registered and make sure your broadcast is a global) and from broadcast you can start a service which will show activity. – yashkal Jul 18 '17 at 13:09