0 votos

¡El gestor de alarmas con servicio de fondo no funciona!

¿alguien puede resolver el problema? este es mi código FICHERO MANIFIESTO

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

ACTIVIDAD PRINCIPAL

public class MainActivity extends Activity {

private WebView mWebView;
private Context context;

private WebView mWebView;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.context = this;
    Intent alarm = new Intent(this.context, AlarmReceiver.class);
    boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
    if(alarmRunning == false) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000, pendingIntent);
    }
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("https://entropay.com/");
    mWebView.setWebViewClient(new com.husdon.background.MyAppWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
            // Force links and redirects to open in the WebView instead of in a browser
            mWebView.setWebViewClient(new WebViewClient());
        }});

}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

}

RECEPTOR DE ALARMAS.JAVA public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent background = new Intent(context, BackgroundService.class);
    context.startService(background);
}

}

SERVICIO DE FONDO.JAVA

public class BackgroundService extends Service {

private boolean isRunning;
private Context context;
private Thread backgroundThread;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    this.context = this;
    this.isRunning = false;
    this.backgroundThread = new Thread(myTask);
}

private Runnable myTask = new Runnable() {
    public void run() {
        // Do something here
        stopSelf();
    }
};

@Override
public void onDestroy() {
    this.isRunning = false;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(!this.isRunning) {
        this.isRunning = true;
        this.backgroundThread.start();
    }
    return START_STICKY;
}

}

0voto

Nirmal Mandal Puntos 1

En lugar de private Runnable myTask = new Runnable() { public void run() { // Haz algo aquí stopSelf(); }

intente private Runnable myTask = new Runnable() { public void run() { // Haz algo aquí //Código aquí para la alarma }

PreguntAndroid.com

PreguntAndroid es una comunidad de usuarios de Android en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X