Android 我的发球随机结束

Android 我的发球随机结束,android,android-studio,service,android-service,Android,Android Studio,Service,Android Service,我提供了一项服务,当我登录我的应用程序时,服务将由以下代码启动 Intent i=newintent(MenuPrincipalUsuarios.this,UsuarioService.class); i、 putExtra(“Idusario”,Idusario); i、 putExtra(“nombreUsuario”,nombreUsuario); i、 putExtra(“apellidoUsuario”,apellidoUsuario); i、 putExtra(“emailUsuar

我提供了一项服务,当我登录我的应用程序时,服务将由以下代码启动
Intent i=newintent(MenuPrincipalUsuarios.this,UsuarioService.class);
i、 putExtra(“Idusario”,Idusario);
i、 putExtra(“nombreUsuario”,nombreUsuario);
i、 putExtra(“apellidoUsuario”,apellidoUsuario);
i、 putExtra(“emailUsuario”,emailUsuario);
i、 putExtra(“telefonoUsuario”,telefonoUsuario);
startService(一)

一切都很好,但最终服务结束了,我不知道为什么。有时是在我登录10小时后,有时是15分钟后服务关闭。 为什么这项服务对我如此重要而从未关闭?因为我在这个服务中有一个处理程序,如果我的服务器的表中出现一些新数据,它会每10秒监听一次。 服务类别为:

public class UsuarioService extends Service {

final Handler handler = new Handler();
JSONArray datosNotificacion = null;
String idCancha, anio, mes, dia, idNotificacion;
String mensaje;
String titulo;
String idOrigen;
String idUsuario;
String nombreUsuario;
String apellidoUsuario;
int diaHoy, mesHoy, anioHoy;
String emailUsuario;
String telefonoUsuario;

private static String url_fecha_actual= "http://hosting/get_dia_mes_anio_actual.php";


private static final String TAG_SUCCESSFECHA = "success";
private static final String TAG_FECHA = "fecha";
private static final String TAG_DIAHOY= "dia";
private static final String TAG_MESHOY= "mes";
private static final String TAG_ANIOHOY= "anio";
private static final String TAG_HORAREDONDEADA= "horaRedondeada";
String horaRedondeada;
private ProgressDialog pDialog;
JSONArray fecha = null;


String nombreOrigen;
//url para recibir notificacion
private static String url_notificacion = "http://hosting/get_notificacion?idUsuarioActivo=";
private static String url_delete_notificacion = "http://hosting/delete_notificacion.php?idNotificacion=";
private static final String TAG_SUCCESS = "success";
private static final String TAG_DATOSNOTIFICACION = "datosNotificacion";
private static final String TAG_IDNOTIFICACION = "idNotificacion";
private static final String TAG_IDORIGEN = "idOrigen";
private static final String TAG_NOMBREORIGEN = "nombreOrigen";
private static final String TAG_TITULO = "titulo";
private static final String TAG_MENSAJE= "mensaje";
private static final String TAG_DIA = "dia";
private static final String TAG_MES = "mes";
private static final String TAG_ANIO = "anio";
private static final String TAG_IDCANCHA = "idCancha";





@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    idUsuario = intent.getStringExtra("idUsuario");
    nombreUsuario = intent.getStringExtra("nombreUsuario");
    apellidoUsuario = intent.getStringExtra("apellidoUsuario");
    emailUsuario = intent.getStringExtra("emailUsuario");
    telefonoUsuario = intent.getStringExtra("telefonoUsuario");
    runnable.run();
    return Service.START_NOT_STICKY;

}

@Override
public IBinder onBind(Intent intent) {

    //TODO for communication return IBinder implementation
    return null;
}

//Runnable para generar la accion de buscar notificacion cada X minutos
Runnable runnable = new Runnable() {
    public void run() {
        if(isConnectingToInternet(getApplicationContext())){
            new BuscarNotificaciones().execute();
        }
        handler.postDelayed(runnable, 10000);
    }
};

private boolean isConnectingToInternet(Context applicationContext){
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        // There are no active network
        return false;
    } else
        return true;
}


class BuscarNotificaciones extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    protected String doInBackground(String... args) {
        // Obtenemos el string del JSON desde la URL
        JSONObject json2 = null;
        try {

            json2 = JSONParser.readJsonFromUrl(url_notificacion.concat(idUsuario));
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        try {
            // Verifica que se cargaron datos
            //assert json2 != null;
            int success;// = json2.getInt(TAG_SUCCESS);
            if(json2 == null){
                success = 0;
            }
            else {
                success = json2.getInt(TAG_SUCCESS);
            }
            if (success == 1) {
                Random random = new Random();
                int m = random.nextInt(9999 - 1000) + 1000;

                datosNotificacion= json2.getJSONArray(TAG_DATOSNOTIFICACION);

                JSONObject c2 = datosNotificacion.getJSONObject(0);
                idNotificacion = c2.getString(TAG_IDNOTIFICACION);
                idOrigen = c2.getString(TAG_IDORIGEN);
                nombreOrigen = c2.getString(TAG_NOMBREORIGEN);
                titulo = c2.getString(TAG_TITULO);
                mensaje= c2.getString(TAG_MENSAJE);
                dia= c2.getString(TAG_DIA);
                mes= c2.getString(TAG_MES);
                anio= c2.getString(TAG_ANIO);
                idCancha= c2.getString(TAG_IDCANCHA);
                //uri donde esta el sonido por defecto de la notificacion
                Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon_large);

                Intent intent = new Intent(UsuarioService.this, CalcularHorariosDisponibles.class);
                intent.putExtra("idComplejo",idOrigen);
                intent.putExtra("idUsuario", idUsuario);
                intent.putExtra("idCancha",idCancha);
                intent.putExtra("dia",dia);
                intent.putExtra("mes",mes);
                intent.putExtra("anio",anio);
                intent.putExtra("nombreUsuario", nombreUsuario);
                intent.putExtra("apellidoUsuario", apellidoUsuario);
                intent.putExtra("diaHoy", String.valueOf(diaHoy));
                intent.putExtra("horaRedondeada", horaRedondeada);

                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(
                                UsuarioService.this,
                                m,
                                intent,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );//creamos la notificacion
                NotificationCompat.Builder n  = new NotificationCompat.Builder(UsuarioService.this)
                        .setContentTitle(titulo)
                        .setSound(uri)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setLargeIcon(bm)
                        .setContentIntent(resultPendingIntent)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("El complejo " + nombreOrigen + " " + mensaje))
                        .setAutoCancel(true);
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(m, n.build());
                //eliminamos la notificacion para que no se sigan generando las notificaciones
                JSONObject json3 = null;
                try {
                    json3 = JSONParser.readJsonFromUrl(url_delete_notificacion.concat(idNotificacion));
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {}
}

@Override
public void onDestroy() {
    handler.removeCallbacks(runnable);
    super.onDestroy();
}
}
公共类服务扩展服务{
最终处理程序=新处理程序();
JSONArray DatosNotification=null;
字符串idCancha、anio、mes、dia、IDNotification;
弦门萨结;
提特罗串;
弦长;
字符串idUsuario;
字符串nombreUsuario;
弦环;
内特迪亚霍伊、米霍伊、阿尼霍伊;
字符串:usuario;
弦乐;
私有静态字符串url\u fecha\u实际值=”http://hosting/get_dia_mes_anio_actual.php";
私有静态最终字符串标记_SUCCESSFECHA=“success”;
私有静态最终字符串标记\u FECHA=“FECHA”;
私有静态最终字符串标记_DIAHOY=“dia”;
私有静态最终字符串标记_MESHOY=“mes”;
私有静态最终字符串标记_ANIOHOY=“anio”;
私有静态最终字符串标记\u horredondeada=“horredondeada”;
霍雷东达达;
私人对话;
JSONArray-fecha=null;
字符串诺姆列根;
//url参数recibir通知
私有静态字符串url_notification=”http://hosting/get_notificacion?idUsuarioActivo=";
私有静态字符串url_delete_notification=”http://hosting/delete_notificacion.php?idNotificacion=";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_datosNotification=“datosNotification”;
私有静态最终字符串标记_idnotification=“idnotification”;
私有静态最终字符串标记_IDORIGEN=“IDORIGEN”;
私有静态最终字符串标记_NOMBREORIGEN=“NOMBREORIGEN”;
私有静态最终字符串标记_TITULO=“TITULO”;
私有静态最终字符串标记\u MENSAJE=“MENSAJE”;
专用静态最终字符串标记_DIA=“DIA”;
专用静态最终字符串标记_MES=“MES”;
私有静态最终字符串标记_ANIO=“ANIO”;
私有静态最终字符串标记\u IDCANCHA=“IDCANCHA”;
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//做一些有用的事情
idUsuario=intent.getStringExtra(“idUsuario”);
nombreUsuario=intent.getStringExtra(“nombreUsuario”);
apellidoUsuario=intent.getStringExtra(“apellidoUsuario”);
emailUsuario=intent.getStringExtra(“emailUsuario”);
telefonoUsuario=intent.getStringExtra(“telefonoUsuario”);
runnable.run();
退货服务。开始时不粘;
}
@凌驾
公共IBinder onBind(意向){
//通信返回IBinder实现的TODO
返回null;
}
//可运行的客车通知系统
Runnable Runnable=新的Runnable(){
公开募捐{
如果(正在连接到Internet(getApplicationContext()){
新总线通知().execute();
}
handler.postDelayed(可运行,10000);
}
};
专用布尔值连接到Internet(上下文应用程序上下文){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo ni=cm.getActiveNetworkInfo();
如果(ni==null){
//没有活动网络
返回false;
}否则
返回true;
}
类BusCarNotifications扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…args){
//Obtenemos el string del JSON desde la URL
JSONObject json2=null;
试一试{
json2=JSONParser.readJsonFromUrl(url_notification.concat(idUsuario));
}捕获(IOException | JSONException e){
e、 printStackTrace();
}
试一试{
//卡加隆达托斯酒店
//断言json2!=null;
int success;/=json2.getInt(TAG_success);
if(json2==null){
成功=0;
}
否则{
success=json2.getInt(TAG_success);
}
如果(成功==1){
随机=新随机();
int m=random.nextInt(9999-1000)+1000;
datosNotification=json2.getJSONArray(TAG_datosNotification);
JSONObject c2=DatosNotification.getJSONObject(0);
IDNotification=c2.getString(标记为IDNotification);
idOrigen=c2.getString(TAG_idOrigen);
nombreOrigen=c2.getString(TAG_nombreOrigen);
titulo=c2.getString(TAG_titulo);
mensaje=c2.getString(TAG_mensaje);
直径=c2.getString(标签直径);
mes=c2.getString(TAG_mes);
anio=c2.getString(TAG_anio);
idCancha=c2.getString(TAG_idCancha);
//我不知道该怎么做
Uri Uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
位图bm=BitmapFactory.decodeResource(getResources(),R.drawable.notification\u icon\u large);
Intent Intent=新的Intent(UsuarioService.this,CalcularHorariosDisponibles.class);
意向。额外(“idComplejo”,idOrigen);
意图。额外(“idUsuario”,idUsuario);
意向。额外(“idCancha”,idCancha);
意向。额外(“直径”,直径);
意向。额外(“mes”,mes);
意图
<activity
        android:name=".MenuPrincipalUsuarios"
        android:label="¡Bienvenido!"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme.Dark" />

<service
        android:name="com.sourcey.modeloUsuarios.UsuarioService"
        android:icon="@drawable/notification_icon"
        android:label="Servicio"
        >
</service>