目前正在使用BroadcastReceiver构建一个Android应用程序,它是一个扫描唯一SSID的WifiDetection应用程序,我的通知工作正常,但总是每1秒触发一次(这很烦人),我希望它延迟15分钟或30分钟。这是我的一些代码。 我使用过Thread.sleep()和处理程序,但它会减慢并崩溃我的应用程序
li
}
public class WifiReceiver extends BroadcastReceiver {
private void receiveNotification(Context context, String ssid) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.drawable.wifilogosmaller);
builder.setContentTitle("Free " + ssid + ", Click for more info");
builder.setContentText("Wifi.com.ng services avaliable ");
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.wifilogosmaller));
builder.setSubText("Tap to view");
builder.setContentIntent(pendingIntent);
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
Notification notification = builder.build();
NotificationManager mgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mgr.notify(1001, notification);
提前致谢...
答案 0 :(得分:0)
@Override
public void onReceive( Context context, Intent intent)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
if(!prefs.getBoolean("isNotificationFired", false))
{
receiveNotification(Context context, "My unique SSID")
prefs.edit().putBoolean("isNotificationFired", true).commit();
//start service here
Intent intent=new Intent(context, MyService.class);
context.startService(intent);
}
}
private void receiveNotification(Context context, String ssid) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.drawable.wifilogosmaller);
builder.setContentTitle("Free " + ssid + ", Click for more info");
builder.setContentText("Wifi.com.ng services avaliable ");
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.wifilogosmaller));
builder.setSubText("Tap to view");
builder.setContentIntent(pendingIntent);
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
Notification notification = builder.build();
NotificationManager mgr = (NotificationManager)
context.getSystemService(NOTIFICATION_SERVICE);
mgr.notify(1001, notification);
}
制作自己的服务,这将改变15分钟后保存在pref中的布尔值“isNotificationFired”的状态......
public class MyService extends Service {
private final LocalBinder mBinder = new LocalBinder();
protected Handler handler;
public class LocalBinder extends Binder {
public MyServicegetService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
handler.postDelayed(runnable, 15*60*1000);//**15 min(time in millisecond)**
return Service.START_STICKY;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("isNotificationFired", true).commit();
}
};