Web服务调用在BroadcastReceiver上不起作用

时间:2018-12-07 06:36:06

标签: java android

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);
    }
}
  

它显示无法启动接收器com.example.agniva.demoapp.Alarm:java.lang.ClassCastException:android.app.ReceiverRestrictedContext无法转换为com.example.agniva.demoapp.PaymentServiceListener

付款服务在此处找不到上下文。可以在这里调用网络服务吗?请给我任何建议或链接。还是有其他方法可以在AlarmManager中调用Web服务。

1 个答案:

答案 0 :(得分:0)

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