私は次のコードを持っています。基本的には、Webサービスから10秒ごとにデータを取得していて、警告ダイアログと通知に毎回何が表示されます。問題は、アラートが前のサイクルから終わっておらず、ページにまだ残っていない場合、アラートをポップアップしないようにする方法です。そのため、ユーザーが閉じる必要があるアラートが2つ以上表示されることはありません。Androidの警告ダイアログisShowingプロパティは常にfalseです
public class myActivity extends Activity {
public class getActiveAlerts extends Thread {
public void run() {
while (true)
{
try {
.... code to get data from web service .....
それは次の10秒間はまだ偽示し、それは新しいを作成する「alert.isShowing」、古いアラートがまだそこにあるにもかかわらず、来るときAlertDialogは、現在および非触れた場合であっても、のように思えます同じプロパティで10秒ごとに警告します。 (アラートが開かれていると思っていましたが、alert.isShowingは真実を示し、新しいアラートを作成しませんでしたか?
最後に、通知が現在アクティブであるかどうかを検出する方法があると信じていますが、10秒後にスレッドが再び開始されたときに通知が表示されているかどうかを確認する標準的な方法はありますか? 、通知は10秒ごとに作成されませんので、?
String root = doc.getDocumentElement().getNodeName();
//lets set alert title and msg to whatever "root" is for now, root contains the data I get from my web service.
myActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder alertBuild = new AlertDialog.Builder(myActivity.this);
alertBuild.setMessage(root)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = alertBuild.create();
if(alert.isShowing())
{
Log.v("alert", "isShowing==true");
}
else {
Log.v("alert", "isShowing==false");
alert.setTitle(root);
alert.setIcon(R.drawable.ic_launcher);
alert.show();
}
}
});
displayNotification(root, root);
}
}
}
}catch (SAXParseException err) {
Log.v(" " , err.getMessage());
}catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}catch (Throwable t) {
t.printStackTrace();
}
try {
Thread.sleep(10000); // sleeps for 10 seconds then runs myActivity again
} catch (InterruptedException e) {
e.printStackTrace();
}
}// close while loop
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActiveAlerts thread = new getActiveAlerts();
thread.start();
}
public void displayNotification(String msg, String desMsg)
{
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, msg + "\n" + desMsg, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, new Intent(this, myActivity.class), 0);
notification.setLatestEventInfo(this, "new alert", "", contentIntent);
manager.notify(1, notification);
}