2012-01-02 44 views
2

我要实现以下情形:当用户按下Home键的通知显示在状态栏和应用程序通常隐藏方向变化和通知

。用户点击通知应用程序时通常会恢复。

这里是我的这段代码:

private int NOTIFICATION = R.string.local_service_started; 
private NotificationManager mNM; 

private void showNotification() { 
    CharSequence text = getText(R.string.local_service_started); 
    Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis()); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, JingleCartoucheActivity.class), 0); 
    notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent); 
    mNM.notify(NOTIFICATION, notification); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    if (!isFinishing()) { 
     showNotification(); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mNM.cancel(NOTIFICATION); 
} 

所有这一切,除了一件事正常工作:

当应用程序被运行,用户旋转手机时,创建通知,立即销毁。有没有办法检测onPause()中的手机方向更改并且不显示此通知?

回答

1

您可以使用getResources().getConfiguration().orientation获取当前的方向。在onCreate处,您存储方向值,然后您可以稍后进行比较。请务必将该值存储到一个静态变量中,因为当您旋转手机时,该活动将被销毁,其值也会被破坏。

1

当用户旋转活动被破坏并重新创建的屏幕,如果你想显示方向变更后通知做出的onDestroy方法静态标志,并检查它在onCreate方法来显示像

public void onDestroy() { 
Flag = 1; 
} 
通知

and

public static int Flag = 0; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(Flag == 1) { 
    showNotification(); 
    } 
    else { 
    .......// 
    } 
}