1
上午每隔15分钟目前正在建设使用广播接收器Android应用程序触发,这是一个WifiDetection应用程序,它可以扫描一个独特的SSID,我的通知工作正常,但始终触发每1秒(这是恼人的),我希望延迟15分钟或30分钟。这里是我的一些代码。 我已经使用Thread.sleep()方法和处理程序,但它减缓和崩溃我的应用程序如何推迟NofificationManager在安卓
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);
}
@Override
public void onReceive(Context context, Intent intent)
{
//Checked for the particular SSID am looking for
if(condition is met){
receiveNotification(Context context, "My unique SSID")
}
}
在此先感谢...
我想你的代码,但它没有按预期方式工作,它只是暂停执行开始,任务启动时,它会继续运行每1秒的时间。 –
请使用相同的代码,但使用IntentService而不是扩展Service ...并写入 ssid = intent.getStringExtra(“ssid”); handler = new Handler(); handler.postDelayed(可运行,15 * 60 * 1000); // ** 15分钟(在毫秒时间)** 返回Service.START_STICKY; 里面onHandleIntent方法 –
onHandleIntent返回void而不是int,所以START_STICKY抛出一个错误。使用您的代码而不使用返回类型会导致应用程序崩溃。 –