2017-09-30 17 views
-1

我要当引导运行应用程序服务器允许接收消息的任何时间如何在启动时开始应用,并允许运行像WhatsApp的

不显示应用

这段代码的问题,当启动显示通知两次只 但我想收到消息的任何时间

这是“机器人:出口”重要的是,用什么

代码AndroidManifest

 <service 
     android:name=".appService" 
     android:exported="true" 
     android:enabled="true"> 
     </service> 

    <receiver 
     android:name=".ServiceStarterBoot" 
     android:enabled="true" 
     android:exported="true"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </receiver> 

代码广播接收器

public class ServiceStarterBoot extends BroadcastReceiver { 

private Context context ; 
@Override 
public void onReceive(Context context1, Intent intent) { 
    this.context = context1; 

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
     Intent serviceLauncher = new Intent(context, appService.class); 
     context.startService(serviceLauncher); 
    }} 

代码服务

public class appService extends Service {  
@Override 
public void onCreate() { 
    super.onCreate(); 
    lood(); 
} 
    @Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
    //return mBinder; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show(); 
    return super.onStartCommand(intent, flags, startId); 
} 


private void lood(){ 
    SystemClock.sleep(3000); 

    Runnable runnable = new Runnable() { 
     public void run() { 
      boolean p= true ; 
      while(p) { 
       SystemClock.sleep(5000); 
       showNotification(); 
      } 
     } 
    }; 
    Thread mythread = new Thread(runnable); 
    mythread.start(); 
} 

请帮助 感谢

回答

0

你必须让你的服务粘性:

Service.START_STICKY

那么它是无限的,并且由于任何原因被操作系统杀死时重新启动。

它甚至在启动OS和应用程序不被用户打开时自动启动。

此致敬礼。

+0

启动后只显示通知两次,我该怎么办? – emad

+0

这是正常行为。你的服务的任务是制作一个线程。服务完成它然后任务完成。您必须实现服务的侦听器。例如状态改变监听器。当一个状态改变时。该服务将获得状态并激发您的方法。 – Dareyou

+0

程序打开并且服务器正在运行时没有问题, 问题是服务器在启动时运行 – emad

0

伪代码:

onStartCommand(..){ 

...

//e.g。 wifilisterner Wifilistener WF = new Wifilistener(Interface yourInterfaceCallBackMethod);

}

这样的事情。

相关问题