2015-03-17 3 views
0

Я хочу начать сервис, когда питание подключено к моему устройству и покажет уведомление, но мое приложение ничего не делает или я не делаю то, что происходит, поэтому, пожалуйста, помогите мне, я новичок в мире Android, возможно, я скучаю что-то, миль код выглядит следующим образом:Как запустить мой сервис?

Мой манифест:

<receiver android:name="com.tlm.grhs_client.WiFiMonitor"> 
 
    <intent-filter> 
 
     <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> 
 
    </intent-filter> 
 
</receiver>

Мои Broadcast Ресивер:

public class WiFiMonitor extends BroadcastReceiver { 
 
    @Override 
 
    public void onReceive(Context context, Intent intent) {   
 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
 
     NetworkInfo info = cm.getActiveNetworkInfo(); 
 
     if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) { 
 
      if (info.isConnected()) { 
 
       //iniciar servicio 
 
       Intent serviceIntent = new Intent(context, ClientService.class); 
 
       context.startService(serviceIntent); 
 
      } 
 
      else { 
 
       //parar servicio 
 
     \t Intent serviceIntent = new Intent(context, ClientService.class); 
 
       context.stopService(serviceIntent); 
 
      } 
 
     } 
 
    } 
 
}

и мой класс обслуживания заключается в следующем:

public class ClientService extends Service { 
 

 
\t @Override 
 
\t public IBinder onBind(Intent intent) { 
 
\t \t throw new UnsupportedOperationException("Not yet implemented"); 
 
\t } 
 

 
\t @Override 
 
\t public void onCreate() { 
 
\t \t Toast.makeText(this, "Servicio fue creado", Toast.LENGTH_LONG).show(); 
 
\t } 
 

 
\t @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
 
\t @SuppressLint("NewApi") 
 
\t @Override 
 
\t public void onStart(Intent intent, int startId) { 
 
\t \t // Perform your long running operations here. 
 
\t \t Toast.makeText(this, "Servicio iniciado", Toast.LENGTH_LONG).show(); 
 
\t \t 
 
\t \t Notification.Builder notiBuilder = new Notification.Builder(this); 
 
\t \t notiBuilder.setContentTitle("Prueba"); 
 
\t \t notiBuilder.setContentText("Esto es una prueba"); 
 
\t \t Notification notificacion = notiBuilder.build(); 
 
\t \t 
 
\t \t NotificationManager manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE); 
 
\t \t manager.notify(1, notificacion); 
 
\t } 
 

 
\t @Override 
 
\t public void onDestroy() { 
 
\t \t Toast.makeText(this, "Servicio destruido", Toast.LENGTH_LONG).show(); 
 
\t } 
 

 
}

я включаю WiFi и установить соединение, но уведомление не показывает.

ответ

0

Android уведомления таким образом, требуют небольшой значок, вместе с заголовком и текстом в соответствии с документацией на http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Добавить иконку, но в противном случае код (на быстрый взгляд) выглядит, как она должна работать.

+0

Да, loos вроде бы должен работать, но он не работает, я уже написал в своем коде маленькую и большую иконку уведомления, но все равно он не работает. – user4678772