Error: Service Intent must be explicit

1

Hello, I'm having the following error on android, can someone help me?

Erro: Service Intent must be explicit: Intent { act=SERVICEPUSHNET }

Code:

SMART.JAVA

package imm.pt.immsmart;

            import android.content.Intent;
            import android.support.v7.app.ActionBar;
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.util.Log;
            import android.widget.ProgressBar;
            import android.widget.TextView;

            import org.json.JSONException;
            import org.json.JSONObject;

            public class SMART extends AppCompatActivity {
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_smart);
                    ActionBar actionBar = getSupportActionBar();
                    actionBar.hide();
                    callServer("get-consumo", "");
                    // Service
                    Intent it = new Intent("SERVICEPUSHNET");
                    startService(it);
                }
                private void degenerateJSON(final String data){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TextView apre = findViewById(R.id.apre);
                            ProgressBar progress = findViewById(R.id.percentagem);
                            TextView mbfalta = findViewById(R.id.mbfalta);
                            TextView total = findViewById(R.id.total);
                            TextView pacote = findViewById(R.id.pacote);
                            try{
                                JSONObject jo = new JSONObject(data);
                                apre.setText(jo.getString("apre"));
                                progress.setProgress(jo.getInt("percentagem"));
                                mbfalta.setText(jo.getString("mbfalta"));
                                total.setText(jo.getString("total"));
                                pacote.setText(jo.getString("pacote"));

                            }catch(JSONException e) {e.printStackTrace();}
                        }
                    });

                }
                private void callServer(final String method, final String data){
                    new Thread(){
                        public void run() {
                            String answer = HttpConnection.getSetDataWeb("http://192.168.1.70/myIMM/api.php", method, data);
                                degenerateJSON(answer);
                        }
                    }.start();
                }
            }

ServicoPush.java

package imm.pt.immsmart;

                import android.app.Notification;
                import android.app.NotificationManager;
                import android.app.Service;
                import android.content.Intent;
                import android.graphics.BitmapFactory;
                import android.media.Ringtone;
                import android.media.RingtoneManager;
                import android.net.Uri;
                import android.os.IBinder;
                import android.support.v4.app.NotificationCompat;
                import java.util.ArrayList;
                import java.util.List;

                public class ServicoPush extends Service{

                    public List<Worker> threads = new ArrayList<Worker>();

                    @Override
                    public IBinder onBind(Intent intent) {
                        return null;
                    }
                    @Override
                    public void onCreate(){
                        super.onCreate();
                    }
                    @Override
                    public int onStartCommand(Intent intent, int flags, int startId){
                        Worker w = new Worker(startId);
                        w.start();
                        threads.add(w);
                        return(super.onStartCommand(intent, flags, startId));

                    }
                    class Worker extends Thread{
                        public int startId;
                        public boolean ativo = true;

                        public Worker(int startId){
                            this.startId = startId;
                        }

                        public void run(){
                            while (ativo){
                                try {
                                    Thread.sleep(3000);
                                    //Notificação
                                    notifica();
                                    ativo = false;
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    @Override
                    public void onDestroy(){
                        super.onDestroy();
                        for(int i = 0, tam = threads.size(); i < tam; i++){
                            threads.get(i).ativo = false;
                        }
                    }
                    public void notifica(){
                        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                        builder.setTicker("Limite de Dados Moveis");
                        builder.setContentTitle("IMM SMART");
                        builder.setContentText("Estas quase a atingir o teu limite de dados!");
                        builder.setSmallIcon(R.drawable.ic_launcher_background);
                        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background));
                        Notification n = builder.build();
                        n.vibrate = new long[]{150, 300, 150, 600};
                        nm.notify(R.drawable.ic_launcher_background, n);
                        try{
                            Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            Ringtone toque = RingtoneManager.getRingtone(this, som);
                            toque.play();
                        }catch (Exception e) {e.printStackTrace();}
                    }
                }

Android Manifests:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="imm.pt.immsmart">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SMART">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".ServicoPush" android:label="SERVICEPUSHNET" android:exported="false">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="SERVICEPUSHNET" />
        </intent-filter>
    </service>
    <receiver android:name=".BroadCastPush" android:label="BroadCastPush" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

    
asked by anonymous 22.08.2018 / 10:40

1 answer

4

For security reasons services must be started explicitly and should not have declared attempted . This exception is thrown from Android 5.0.

Declare your service like this:

<service android:name=".ServicoPush"/>

Start it like this:

Intent serviceIntent = new Intent(this, ServicoPush.class);
startService(serviceIntent);

Also note the limitations imposed by Android 8 on running background services .

    
22.08.2018 / 12:10