1
BroadcastReceiverを使ってアンドロイドアプリケーションを構築していますが、ユニークなSSIDをスキャンするWifiDetectionアプリケーションですが、通知はうまくいきますが、それが15分または30分遅れることを望む。ここに私のコードのいくつかがあります。 私はのThread.sleep()およびハンドラを使用しているが、それは自分のアプリケーションNofificationManagerをアンドロイドで15分ごとに起動する方法を遅らせる方法
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を使用してください。 ssid = intent.getStringExtra( "ssid"); ハンドラ=新しいハンドラ(); handler.postDelayed(実行可能、15 * 60 * 1000); // ** 15分(ミリ秒単位の時間)** return Service.START_STICKY; onHandleIntentメソッド内 –
onHandleIntentはintではなくvoidを返します。したがって、START_STICKYはエラーをスローします。戻り値の型を持たないコードを使用すると、アプリがクラッシュします。 –