Open 2nd activity from a service with mainactivity turned off

0

Well, I have a problem and I can not find a solution, I've tried everything and either one or I get the EOF of the application or the screen is all black and does not open to activity.

The service is opening videos or web pages, for a certain amount of time for the user to observe and at the end of the list of results to see, will have to answer certain questions about the content viewed. By means of the means it is also intended to introduce an activity with publicity only to shuffle.

The main activity sends its context to the singlton class (I think this is the name used) GesteDados, to be recognized in the service. Service code:

package net.browser.mynet.mostralista;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;


import net.browser.mynet.Activitis.Mynet_app;
import net.browser.mynet.Activitis.Pub;
import net.browser.mynet.gestdados.GestDados;
import net.browser.mynet.listaSites.Sites;


import java.util.List;


public class MostraLista extends Thread {

//classe mostra lista


    public int startId;
    public boolean ativo = true;
    private GestDados gDados;
    private List <Sites> listaA;

    private Context contexts;

    private int contpas = 0;
    private int pubpas=2;
    private boolean ciclo = true;
    private boolean passagem=true;
//private Mynet_app app;
private Thread th;


    private Long tempo;

    public MostraLista(int startIds) {
        startId = startIds;
        gDados=GestDados.getInstance();
        listaA = gDados.getLista();
        contexts=gDados.getContext();
    }


    public void Run(){ //throws Exception{
        try{
            String novocaminho;
           long milstempo=0;
         final Intent it;
         Intent processo;
            Toast.makeText( contexts, "abre pub" + String.valueOf( listaA.size() ), Toast.LENGTH_LONG ).show();
            while (ciclo){    //(true) {


                if (passagem) {


                    if (contpas < listaA.size()) {
                        passagem = false;
                        novocaminho = listaA.get( contpas ).getCaminho();
                        Log.i( "mostralista - ", listaA.get( contpas ).getCaminho() );



                        if (contpas == pubpas) {
                            Toast.makeText( contexts, "entrou no run abre pub", Toast.LENGTH_LONG ).show();
                           processo = new Intent( Intent.ACTION_VIEW );
                            //processo.setClass( contexts , Mynet_app.class);
                           // processo.setClass( contexts ,Pub.class);
                            processo.setClass( contexts ,Pub.class);
                          //  processo.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                             //Intent intent =new Intent(contexts,Mynet_app.class );//Pub.class
                            //processo.addFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                            //Intent.addFlags( Intent.);
                          //  Intent intent = new Intent( Mynet_app.FILTRO_KEY);
                            //intent.putExtra( Mynet_app.MENSAGEM_KEY,"pub" );
                            //LocalBroadcastManager
                              //      .getInstance(contexts)
                                //    .sendBroadcast( intent);

                          gDados.setLoadPub( false );
                            pubpas = pubpas + 2;
                            milstempo = 60000;

                            contexts.startActivity( processo );

                        } else {
                            Toast.makeText( contexts, "entrou no run abre site", Toast.LENGTH_LONG ).show();
                            processo = new Intent( Intent.ACTION_VIEW );
                            processo.setData( Uri.parse( novocaminho ) );
                            processo.addFlags( Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                            //processo.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                            processo.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            processo.addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            gDados.setLoadPub( true );
                            // processo.addFlags( Intent.FLAG_ACTIVITY_NO_HISTORY );
                            //processo.addFlags( Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
                            //  processo.addFlags( Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
                            //processo.addFlags( Intent.FLAG_ACTIVITY_NO_USER_ACTION );

                            milstempo = (1000 * 60) * listaA.get( contpas ).getTempo(); //.intValue();

                            contpas = contpas + 1;

//.abreit( novocaminho );

                           contexts.startActivity( processo );
                        }
                        
                        //gDados.abreIntent(processo);

                    } else {
                        passagem = false;
                        gDados.OrdenaLista();
                        contpas = 0;
                        pubpas = 2;
                       // gDados.abrepub( 1 );
                        milstempo = 60000;
                        listaA = gDados.getLista();
                        gDados.setLoadPub( true );
                    }


                    tempo = System.currentTimeMillis() + milstempo;
                }
                Long tempo_agora = System.currentTimeMillis();

                Log.i("tempo ", String.valueOf( tempo ));
                Log.i("tempo agora ", String.valueOf( tempo_agora ));

             if (gDados.isLoadPub()) {
                 if (tempo_agora >= tempo) {
                     passagem = true;
                 }
             }
                //try{
                // Log.i("antes do timer ", " passou aqui");
                //timer.schedule( passTemp,milstempo );

                // Thread.sleep( 100 );
                // Log.i("depois do timer ", " passou aqui");
               // Thread.sleep( 1 );
            }

       } catch (Exception ex) {
               ciclo=false;
            Toast.makeText( contexts, "abre pub" + String.valueOf( listaA.size() ), Toast.LENGTH_LONG ).show();
                throw ex;
             }


            }

       //

}

It works fine, the service opens the pages of the browser and youtube as intended but does not open the activity pub. The activity pub's code:

    package net.browser.mynet.Activitis;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import net.browser.mynet.R;
import net.browser.mynet.gestdados.GestDados;

import java.util.Timer;
import java.util.TimerTask;

public class Pub extends AppCompatActivity {
    // Remove the below line after defining your own ad unit ID. "ca-app-pub-6686213999569597~4296619923"
    public static final String FILTRO_KEY = "Pub_KEY";


    private static final String TOAST_TEXT = "Test ads are being shown. " + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";
    private InterstitialAd mInterstitialAd;
    private GestDados gDados=GestDados.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_pub );

        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
try {
    showInterstitial();
}catch (InterruptedException ex){
    Toast.makeText( this, ex.getMessage(), Toast.LENGTH_LONG).show();

}
       // loadInterstitial();


        // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
        Toast.makeText( this, TOAST_TEXT, Toast.LENGTH_LONG ).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
         loadInterstitial();
    }

    private void showInterstitial() throws InterruptedException {
        // Show the ad if it's ready. Otherwise toast and reload the ad.
        boolean leu=false;
      while (!leu){
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            Toast.makeText( this, "Já carregou a pub", Toast.LENGTH_LONG).show();

            gDados.setLoadPub( true );
            leu=true;
        } else {
            Toast.makeText( this, "Ad did not load", Toast.LENGTH_LONG ).show();

            try{
            wait( 1000 );

        }catch (InterruptedException ex){

      Log.i( "Pub",ex.getMessage());
      throw ex;
  }

    }}}

    private void loadInterstitial() {
        // Disable the next level button and load the ad.

        AdRequest adRequest = new AdRequest.Builder().setRequestAgent( "android_studio:ad_template" ).build();
        mInterstitialAd.loadAd( adRequest );
    }


   // private Runnable tarefa = () -> showInterstitial();

}

The service will have to wait for the activity pub to show the ad and only after a certain time does it show the next results. I've done several experiments but I can never open the activity. I have already used a localreceiver and everything and I can not open the activity. I can not figure out what I'm doing wrong.

    
asked by anonymous 13.06.2018 / 14:39

0 answers