0

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

但是,这是行不通的,我不知道我在哪里做错了。

  1. 我在Android清单

<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>

加入服务
  • 我创建三个类 “的BackgroundWorker延伸IntentService”
  • `公共BackgroundWorker(){super(“androidservice”); }

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

    2.类 '通知延伸的BroadcastReceiver'

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

    3.我创建第三类 `龙alertTime =新的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秒Repeatition但我仍然在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

    IntentService在onHandleIntent代码()后停止本身执行。你需要扩展一个普通的服务并自己处理线程。如果您不打电话给stopSelf,服务将继续运行(可能直到系统终止它)。