2013-11-24 37 views
0

我想在顯示警告對話框,但它是目前應用screen.Is荷蘭國際集團再一個辦法做到這一點?顯示警報對話框中的主屏幕

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

是否必須更改PendingIntent變量?

答:

我終於發現做到這一點的最簡單的方法。只需在MainActivity類中添加命令「finish()」即可完成活動並返回主屏幕,然後可以在其中顯示警報。

希望這將有助於其他誰被困在這個問題

+0

閱讀http://stackoverflow.com/questions/7591670/pop-up-dialog-in-android-home-screen和http://stackoverflow.com/問題/ 2705162 /怎麼辦,他們-做它的對話框環比主屏 –

+0

謝謝:)但是,當手機已經opened.Any當手機處於鎖定狀態,只顯示警告,這並不工作幫幫我? – user3008437

+0

看到我的回答,它工作正常 –

回答

0

,如果你想創建在主屏幕上您的應用程序,請參見以下鏈接

thisthis

但如果你想你的代碼以前更加手機使用KeyguardLockWakeLock loocked。以下是我所做的:

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! 
} 
} 

當然,您仍然需要在清單文件中聲明權限。

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

////編輯

,如果你想加入兩項工作必須用本編輯代碼:

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"); 

,並在另一個包或接收器加下面的代碼:

 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(); 
    } 
+0

我試過了。但最初的問題再次出現。屏幕被解鎖,但警告對話框然後顯示在應用程序中而不是屏幕上。感謝您的幫助。 – user3008437

+0

你必須加入此兩項工作,冷杉必須解鎖手機,之後,你在主屏幕模式下設置,你嘗試一下,發佈您的代碼 –

+0

看到我的編輯,我現在測試,其工作過 –

0

使用服務

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

下面是一些代碼

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"); 
    } 

}