像whatsapp

时间:2016-06-16 05:17:45

标签: android broadcastreceiver android-notifications intentservice android-intentservice

我不知道为什么这段代码不起作用,我正在尝试像whatsapp这样的后台服务,它会永远运行并检查通知。

但这不起作用,我不知道我在哪里做错了。

  1. 我在android manifest
  2. 中添加了服务

    <receiver android:name=".Notifications"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> <action android:name="android.intent.action.TIME_SET" /> <action android:name="com.application.Notifications" /> </intent-filter> </receiver> <service android:name=".BackgroundWorker"></service>

    1. 我创建了三个类&#34; BackgroundWorker扩展了IntentService&#34;
    2. `public BackgroundWorker(){         超级(&#34; androidservice&#34);     }

      @Override
      protected void onHandleIntent(Intent intent) {
      
      
      
          Log.v("BackgroundWorker:","working" );
      
          //user method
          createNotification(getApplicationContext(),"BackgroundWorker");
      }`
      

      2。 class&#39;通知扩展了BroadcastReceiver&#39;

      @Override
      public void onReceive(final Context context, Intent intent) {
      
          Log.d("BroadcastReceiver","onReceive");
      
          Intent t = new Intent(context,BackgroundWorker.class);
          context.startService(t);
      
      }
      

      3。我创造了第三堂课 `long alertTime = new GregorianCalendar()。getTimeInMillis()+ 1 * 1000;

          Intent notifications = new Intent(this,Notifications.class);
      
          AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
      
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,alertTime,1*1000,PendingIntent.getBroadcast(this,
                  1,
                  notifications,
                  PendingIntent.FLAG_UPDATE_CURRENT));
      
          Log.d("AlarmManager",alarmManager.toString());`
      

      我不知道我在哪里做错了。

      **虽然我已经为重复设定了1秒但仍然在5分钟的间隔内收到通知**

      我已经检查了android服务,我的应用程序没有运行 请帮帮我...

      06-16 10:25:21.537 9833-9833/ D/BroadcastReceiver: onReceive 06-16 10:25:21.614 9833-12666/D/BroadcastReceiver: createNotification 06-16 10:30:21.245 9833-9833/ D/BroadcastReceiver: onReceive 06-16 10:30:21.376 9833-15746/r D/BroadcastReceiver: createNotification 06-16 10:35:21.209 9833-9833/ D/BroadcastReceiver: onReceive 06-16 10:35:21.283 9833-18645/ D/BroadcastReceiver: createNotification 06-16 10:40:21.179 9833-9833/ D/BroadcastReceiver: onReceive 06-16 10:40:21.224 9833-21479/ D/BroadcastReceiver: createNotification 06-16 10:45:21.188 9833-9833/ D/BroadcastReceiver: onReceive 06-16 10:45:21.246 9833-24302/ D/BroadcastReceiver: createNotification

1 个答案:

答案 0 :(得分:0)

在执行onHandleIntent()中的代码后,IntentService会自行停止。您需要自己扩展正常的服务并处理线程。如果你不调用stopSelf,服务将继续运行(可能直到系统杀死它)。