0

I want to display Alert Dialog in but it is currently ing in application screen.Is re a way to do that ??

Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(
                          getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);

Do I have to change PendingIntent variables?

Answer:

I finally discovered the easiest way to do that. Just added the command "finish()" in the MainActivity class to finish the activity and go back to the homescreen and then the alert can be displayed there.

Hope it would help other who got stuck in this problem

2 Answers2

0

if you want create your app in home screen, see the following link

this and this

but if you want your code worked even your phone is loocked using the KeyguardLock and WakeLock. Below is what I do:

   public class DismissLock extends Activity {

   PowerManager pm;
   WakeLock wl;
   KeyguardManager km;
   KeyguardLock kl;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      Log.i("INFO", "onCreate() in DismissLock");
      pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
       km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
      kl=km.newKeyguardLock("INFO");
      wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
      wl.acquire(); //wake up the screen
      kl.disableKeyguard();// dismiss the keyguard

     setContentView(R.layout.main);

    }

 @Override
 protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release(); //when the activiy pauses, we should realse the wakelock
}

 @Override
 protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    wl.acquire();//must call this!
}
}

Of course, you still need to declare the permission in the manifest file.

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

////EDIT

and if you want join two work you must edit your code with this:

   boolean fDialogMode     = getIntent().hasExtra( "dialog_mode" );

      if( ! fDialogMode ) {
          super.setTheme( android.R.style.Theme_Dialog );
      }

   AlertDemo alert = new AlertDemo();
   pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
   kl=km.newKeyguardLock("INFO");
   wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
   wl.acquire(); //wake up the screen                  
   kl.disableKeyguard();// dismiss the keyguard
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "AlertDemo");

and in another package or receiver add following code:

        Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("com.example.openlock");
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);

        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • I have tried it . but the initial problem arises again.The screen gets unlocked but the alert dialog is then displayed in the application and not on the screen. Thank you for your help. – user3008437 Nov 24 '13 at 07:26
  • you must join this two work, firs you must unlock your phone, and after that you set in home screen mode, what you try, post your code – Shayan Pourvatan Nov 24 '13 at 07:30
0

Use Service for that

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.getApplicationContext().startActivity(intent);

Below is some code

public class HomepopupDataService extends Service {

    private static final String TAG = "HomepopupDataService";

    @Override
    public void onCreate() {
        Log.i(TAG, "Service onCreate");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Service onStartCommand");

        CountDownTimer dlgCountDown;
        Log.e("---------------", "onHandleIntent");
        dlgCountDown = new CountDownTimer(10000, 1000) {
            public void onTick(long millisUntilFinished) {
                Log.e("---------------", "onHandleIntent++");
            }

            public void onFinish() {
                Intent i = new Intent(getApplicationContext(),
                        DialogActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(i);
            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Service onBind");
        return null;
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "Service onDestroy");
    }

}
A.L
  • 10,259
  • 10
  • 67
  • 98
jigar
  • 307
  • 2
  • 11