设备锁定(睡眠)时广播接收器无效

时间:2014-07-02 08:49:04

标签: android service broadcastreceiver alarmmanager

当我锁定手机时,它不会打电话给api .... 但是当手机未锁定时,它的工作正常.......

在我的 startActivity

public void startUserService()
    {
        try
        {
             PowerManager.WakeLock wl;
             PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ROMS Lock");
            wl.acquire();
            Intent intent = new Intent(Start_Activity.this, PermitService.class);
            intent.putExtra("start", true);
            PendingIntent pintent = PendingIntent.getBroadcast(Start_Activity.this, 0, intent, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); //Constant.serviceTime
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 500, 20000, pintent);
            isServiceRunning = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

和我的 BroadCastReceiver

public class PermitService extends BroadcastReceiver
{

    Context mContext;
    @Override
    public void onReceive(Context context, Intent intent)
    {
        mContext=context;
        if (intent != null && intent.getBooleanExtra("start", false))
        {
            if (new Validations(mContext).Connection_Check())
            {
                Toast.makeText(context, "User Service called ", Toast.LENGTH_SHORT).show();
                setRequest();
            }
        }

    }

    @SuppressLint("ShowToast")
    public void setRequest()
    {

        StringListener objStringListner = new StringListener()
        {

            @Override
            public void onSuccess(String arg0)
            {
                if (arg0.length() > 0)
                {
                    int code = Integer.parseInt(arg0);
                    if (code != 0)
                    {
                        //error message
                    }
                }
            }
        };

        SharedPreferences pref = mContext.getSharedPreferences(Constant.PREF_APP, Context.MODE_PRIVATE);

        ContentValues param = new ContentValues();
        param.put("UserType", pref.getInt("UserType", 0));
        param.put("UserId", pref.getInt("UserId", 0));
        MRequest objRequest = General_Class.checkUserAuth(Method.POST, param, "UpdateCheckLogin", objStringListner, mContext);
        MRequestQueue objRequestQueue = new MRequestQueue(mContext);
        objRequestQueue.add(objRequest);
    }

}

注意: 我已经检查了平板电脑和手机,相同的代码适用于移动设备,但它不适用于平板电脑(micromax,iball)。

0 个答案:

没有答案