Android AlarmManager,广播接收器在某些设备上不工作

Android AlarmManager,广播接收器在某些设备上不工作,android,service,broadcastreceiver,alarmmanager,wakelock,Android,Service,Broadcastreceiver,Alarmmanager,Wakelock,我在以下活动中创建alarmmanager AlarmManager alarmManager=(AlarmManager) MenuItems.this.getSystemService(Context.ALARM_SERVICE); Intent intent23 = new Intent(MenuItems.this, MyReceiver.class); PendingIntent pendingIntent23 = PendingIntent.getBroadc

我在以下活动中创建alarmmanager

    AlarmManager alarmManager=(AlarmManager) MenuItems.this.getSystemService(Context.ALARM_SERVICE);
    Intent intent23 = new Intent(MenuItems.this, MyReceiver.class);
    PendingIntent pendingIntent23 = PendingIntent.getBroadcast(MenuItems.this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),300000,
                                                                          pendingIntent);
在这之后,我有下面的接收机

public class MyReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {

    ComponentName comp = new ComponentName(context.getPackageName(),
            MyAlarmService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}
此广播接收器调用我的以下服务

Public class MyAlarmService extends Service
 {
private String server_response,data_to_send;
private String server ;
private String setLocation =  "/xyz";
private URL url; 
public double mLatitude;
private MainActivity main;
private boolean isGpsEnabled = false;
  private sendToserver send;
  private Context context;
 public String vallatlong;

private boolean isNetworkEnabled = false;

private boolean canGetLocation = false;

private Location mLocation;


public MyAlarmService(Context context) 
{
    this.context = context;

    getLocation();
}
public MyAlarmService() 
{

}

public double mLongitude;
private LocationManager mLocationManager;
 private NotificationManager mManager;

 @Override
 public IBinder onBind(Intent arg0)
 {
   // TODO Auto-generated method stub
    return null;
 }

@Override
public void onCreate() 
{
   // TODO Auto-generated method stub  
   super.onCreate();
}


   @Override
   public void onStart(Intent intent, int startId)
   {
   super.onStart(intent, startId);
    main = new MainActivity();
   getLocation();
  }

    public String getLocation() 
 {
   server = getString(R.string.server_add_new);
   try {
    Log.e("this is getlocation method ", "1");

       mLocationManager = (LocationManager)         getApplicationContext().getSystemService(LOCATION_SERVICE);

       /*getting status of the gps*/
       isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


       /*getting status of network provider*/
       isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

       if (!isGpsEnabled && !isNetworkEnabled) {

           Log.e("no provider", "non availability");
       } else {
           Log.e("this is getlocation method ", "2");
           this.canGetLocation = true;

           /*getting location from network provider*/
           if (isNetworkEnabled) {

              // mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
               Log.e("this is getlocation method ", "3");
               if (mLocationManager != null) {
                   Log.e("this is getlocation method ", "7");
                   mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                   if (mLocation != null) {
                       Log.e("this is getlocation method ", "8");
                       mLatitude = mLocation.getLatitude();

                       mLongitude = mLocation.getLongitude();
                       long user_id = main.getDefaultsint("user_id", MyAlarmService.this);

                       String stringLatitude = String.valueOf(mLatitude);
                       String stringLongitude = String.valueOf(mLongitude);
                       vallatlong = stringLatitude+stringLongitude;

                       try {
                            url = new URL(server+setLocation);
                            data_to_send = "lat=" + mLatitude + "&";
                            data_to_send += "long=" + mLongitude + "&";
                            data_to_send += "user_id=" + user_id ;

                            Log.e("data to send", "url"+data_to_send);
                            send = new sendToserver(url, data_to_send);
                            send.execute();
                              Log.e("this is getlocation method ", "6");
                        } catch (MalformedURLException e) {

                            e.printStackTrace();
                        }
                   }
               }
               /*if gps is enabled then get location using gps*/
               if (isGpsEnabled) {

                   if (mLocation == null) {

                    //   mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
                       Log.e("this is getlocation method ", "4");
                       if (mLocationManager != null) {

                           mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                           if (mLocation != null) {
                               Log.e("this is getlocation method ", "5");

                               mLatitude = mLocation.getLatitude();

                               mLongitude = mLocation.getLongitude();



                               long user_id = main.getDefaultsint("user_id", MyAlarmService.this);

                               try {
                                    url = new URL(server+setLocation);
                                    data_to_send = "lat=" + mLatitude + "&";
                                    data_to_send += "long=" + mLongitude + "&";
                                    data_to_send += "user_id=" + user_id ;

                                    Log.e("data to send", "url"+data_to_send);
                                    send = new sendToserver(url, data_to_send);
                                    send.execute();
                                      Log.e("this is getlocation method ", "6");
                                } catch (MalformedURLException e) {

                                    e.printStackTrace();
                                }
                           }

                       }
                   }

               }
           }
       }

    } catch (Exception e) {

       e.printStackTrace();
    }
       return vallatlong;
 }

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}



public double getLatitude() {

    if (mLocation != null) {

        mLatitude = mLocation.getLatitude();
    }
    return mLatitude;
}


public double getLongitude() {

    if (mLocation != null) {

        mLongitude = mLocation.getLongitude();

    }

    return mLongitude;
}


public boolean canGetLocation() {

    return this.canGetLocation;
}

public class sendToserver extends AsyncTask<URL, String, ArrayList<String>> 
{
    @Override
    protected void onPostExecute(ArrayList<String> result) {

        super.onPostExecute(result);
    }


    String data_to_send = "";
     URL url;
     public sendToserver(URL u, String data){
         this.url =  u;
         this.data_to_send = data;
     }

    @Override
    protected ArrayList<String> doInBackground(URL... params) { 

        BufferedReader reader=null;

      try
      { 


        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true);                   
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
        wr.write(data_to_send);
        wr.flush();     

        // Get the server response 

      reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      StringBuilder sb = new StringBuilder();
      String line = null;

      // Read Server Response
      while((line = reader.readLine()) != null)
          {
                 // Append server response in string
                 sb.append(line + "\n");
                 Log.e("inside", "while loop");
          }


      server_response = sb.toString();


      }
      catch(Exception ex)
      { 

      }

        return null;
    }

}
}
公共类MyAlarmService扩展服务
{
私有字符串服务器响应,数据发送;
私有字符串服务器;
私有字符串setLocation=“/xyz”;
私有URL;
公开双重授权;
私人活动主体;
私有布尔值isGpsEnabled=false;
私有发送到服务器发送;
私人语境;
公共字符串vallatlong;
私有布尔值isNetworkEnabled=false;
私有布尔值canGetLocation=false;
私人位置;
公共MyAlarmService(上下文)
{
this.context=上下文;
getLocation();
}
公共MyAlarmService()
{
}
公众的双重身份;
私人场所经理;
私人通知管理人;
@凌驾
公共IBinder onBind(意图arg0)
{
//TODO自动生成的方法存根
返回null;
}
@凌驾
public void onCreate()
{
//TODO自动生成的方法存根
super.onCreate();
}
@凌驾
公共无效启动(Intent Intent,int startId)
{
super.onStart(intent,startId);
main=新的main活动();
getLocation();
}
公共字符串getLocation()
{
server=getString(R.string.server\u add\u new);
试一试{
Log.e(“这是getlocation方法”,“1”);
mlLocationManager=(LocationManager)getApplicationContext().getSystemService(位置服务);
/*获取gps的状态*/
isGpsEnabled=mlLocationManager.isProviderEnabled(LocationManager.GPS\U提供程序);
/*正在获取网络提供商的状态*/
isNetworkEnabled=mlLocationManager.isProviderEnabled(LocationManager.NETWORK\u提供程序);
如果(!isGpsEnabled&!isNetworkEnabled){
Log.e(“无供应商”、“不可用”);
}否则{
Log.e(“这是getlocation方法”,“2”);
this.canGetLocation=true;
/*从网络提供商处获取位置*/
if(可联网){
//mlLocationManager.requestLocationUpdates(LocationManager.NETWORK\u提供程序,用于更新的最短时间,用于更新的最短距离,此);
Log.e(“这是getlocation方法”,“3”);
if(mLocationManager!=null){
Log.e(“这是getlocation方法”,“7”);
mlLocation=mlLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
如果(mLocation!=null){
Log.e(“这是getlocation方法”,“8”);
mLatitude=mLocation.getLatitude();
mlongalite=mLocation.getLongitude();
long user\u id=main.getDefaultsint(“user\u id”,MyAlarmService.this);
String stringLatitude=String.valueOf(mLatitude);
String stringLongitude=String.valueOf(mLongitute);
vallatlong=弦纬度+弦经度;
试一试{
url=新url(服务器+设置位置);
数据发送=“lat=“+mLatitude+”&”;
数据发送+=“long=“+mlongity+”&”;
数据发送+=“用户id=”+用户id;
Log.e(“要发送的数据”,“url”+要发送的数据);
send=新的sendToserver(url、数据发送);
send.execute();
Log.e(“这是getlocation方法”,“6”);
}捕获(格式错误){
e、 printStackTrace();
}
}
}
/*如果启用了gps,则使用gps获取位置*/
如果(isGpsEnabled){
if(mLocation==null){
//mlLocationManager.requestLocationUpdates(LocationManager.GPS\u提供程序、用于更新的最小时间、用于更新的最小距离、用于更新的最小更改);
Log.e(“这是getlocation方法”,“4”);
if(mLocationManager!=null){
mLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS\U提供程序);
如果(mLocation!=null){
Log.e(“这是getlocation方法”,“5”);
mLatitude=mLocation.getLatitude();
mlongalite=mLocation.getLongitude();
long user\u id=main.getDefaultsint(“user\u id”,MyAlarmService.this);
试一试{
url=新url(服务器+设置位置);
数据发送=“lat=“+mLatitude+”&”;
数据发送+=“long=“+mlongity+”&”;
数据发送+=“用户id=”+用户id;
Log.e(“要发送的数据”,“url”+要发送的数据);
send=新的sendToserver(url、数据发送);
send.execute();
Log.e(“这是getlocation方法”,“6”);
}捕获(格式错误){
e、 printStackTrace();
}
}
}
}
}
}
}
}捕获(例外e){
e、 printStackTrace();
}
返回瓦拉特隆;
}
@凌驾
公共空间
{
//TODO自动生成的方法存根
super.ondestory();
}
公共双纬度(){
如果(mLocation!=null){
mLatitude=mLocation.getLatitude();
}
返回时间;
}
公共双getLongitude(){
如果(mLocation!=null){