首页 > 解决方案 > Web 服务调用在 BroadcastReceiver 上不起作用

问题描述

package com.example.agniva.demoapp;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

public class Alarm extends BroadcastReceiver implements PaymentServiceListener {

    Database database;
    ArrayList<String> log_arrlist;
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("I am in", "Alarm onReceive");
        database = new Database(context);
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Put here YOUR code.
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
        database.add_log("Alarm !!!!!!!!!!" + "\n");
        wl.release();

        Cursor cursor = database.getAllLog();
        log_arrlist = new ArrayList<>();

        if (cursor.moveToFirst()) {
            Log.e("Cursor Object>>>>>>>", DatabaseUtils.dumpCursorToString(cursor));
            do {
                String allLog = cursor.getString(cursor.getColumnIndex("fld_log_name"));
                Log.e("allLog", ">>>" + allLog);
                log_arrlist.add(allLog);

                for (int z = 0; z < log_arrlist.size(); z++) {
                    Log.e("LOG", "ARRAY>>" + log_arrlist.get(z));
                }

                // do what ever you want here
            } while (cursor.moveToNext());
        }
        cursor.close();

        Intent background = new Intent(context, EmergencyService.class);
        context.startService(background);

        /**  Service Call is not working here   */
        PaymentService paymentService = new PaymentService(context, log_arrlist);
        paymentService.execute();

    }

    public void setAlarm(Context context) {

        Log.e("I am in", "Alarm setAlarm");

        /*AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent("com.example.agniva.demoapp.START_ALARM");
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);*/

        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent("com.example.agniva.demoapp.START_ALARM");
        alarmIntent = PendingIntent.getBroadcast(context, 0, i, 0);

        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        5 * 1000, alarmIntent);
    }

    @Override
    public void onGettingPaymentResponse(String result) {
        Log.e("Result", ">>" + result);
    }
}

它显示 Unable to start receiver com.example.agniva.demoapp.Alarm: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.example.agniva.demoapp.PaymentServiceListener

支付服务在这里找不到上下文。可以在这里调用网络服务吗?如果您有任何建议或链接,请给我。或者有没有其他方法可以在 AlarmManager 中调用 Web 服务。

标签: javaandroid

解决方案


Receiver 公开的 Context 不是您期望的常见 Context。即使没有活动或服务,接收者也可以生存,因此上下文不是您的,也不是您的任何一个类。我认为您将 Context 转换为 PaymentServiceListener 但这是非法的。您可以使用该上下文来提取字符串或启动/启动服务/活动/线程,但不能强制转换为您的类之一。


推荐阅读