0
我想在我的應用程序中顯示連續通知。2分鐘後連續通知android
目前我能夠在4秒後自動提示通知,但我想要的是它應該在特定的時間間隔後不斷提高。
以下是我的代碼。
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/notify"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Raise a notification"
android:onClick="notifyMe"
/>
<Button android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear the notification"
android:onClick="clearNotification"
/>
</LinearLayout>
這是我的活動文件
package com.vimaltuts.android.notificationexample;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class NotificationExampleActivity extends Activity {
private static final int NOTIFY_ME_ID=1987;
private int count=0;
private NotificationManager mgr=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
View v = new View(getApplicationContext());
notifyMe(v);
}},4000);
}
@SuppressWarnings("deprecation")
public void notifyMe(View v)
{
@SuppressWarnings("deprecation")
Notification note=new Notification(R.drawable.stat_notify_chat,"Status message!",System.currentTimeMillis());
PendingIntent i=PendingIntent.getActivity(this,0,new Intent(this, NotificationMessage.class),0);
note.setLatestEventInfo(this, "New Email","Unread Conversation", i);
note.number=++count;
note.vibrate=new long[] {500L, 200L, 200L, 500L};
note.flags|=Notification.FLAG_AUTO_CANCEL;
mgr.notify(NOTIFY_ME_ID, note);
}
public void clearNotification(View v) {
mgr.cancel(NOTIFY_ME_ID);
}
}
請告訴我,我應該需要有如果任何額外的代碼?
也可以用service..how?
我嘗試下面的代碼敵人測試的目的,但我以前不工作...誰能告訴我哪裏出錯
以下是我的活動代碼
Intent i= new Intent(NotificationExampleActivity.this,NotificationService.class);
PendingIntent pi=PendingIntent.getService(NotificationExampleActivity.this, 0, i, 0);
AlarmManager alarmMgr=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmMgr.cancel(pi);
alarmMgr.setRepeating(alarmMgr.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(),(10*1000), pi);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
而以下是我的廣播接收器
Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();
我想顯示msg =「Repeating Alarm worked」在一段時間後tervel