Android服务一次又一次地创建

Android服务一次又一次地创建,第1张

概述我有一个按钮,上面写着以下代码 Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 10); Intent intent = new Intent(Formact.this, MyService.class); 我有一个按钮,上面写着以下代码

Calendar cal = Calendar.getInstance();                        cal.add(Calendar.SECOND,10);                        Intent intent = new Intent(Formact.this,MyService.class);                        MyService.pintent = PendingIntent.getService(Formact.this,intent,0);                        MyService.alarm = (AlarmManager) getSy@R_301_6563@Service(Context.ALARM_SERVICE);                                    MyService.alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),5000,MyService.pintent);

现在一旦创建服务,它就必须销毁一个特定的动作,但每次破坏之后它都会再次启动.
这是我的服务类

public class MyService extends Service {public static int counter = 0;public static PendingIntent pintent;public static AlarmManager alarm; Boolean save=false;public MyService() {}@OverrIDepublic IBinder onBind(Intent intent) {    return new  Binder() ;}@OverrIDepublic voID onCreate() {    Toast.makeText(this,"Service was Created",Toast.LENGTH_SHORT).show();   }@OverrIDepublic voID onStart(Intent intent,int startID) {    counter++;    Toast.makeText(this," Service Started" + "  " + counter,Toast.LENGTH_SHORT).show();    SaveForm handler = new SaveForm(getApplicationContext());    handler.setobj(getApplicationContext());    handler.setText(Formact.sendform,Formact.ListString);    handler.stratConnection();    String m = "";    int val = 0;    try{        Log.e("val",SaveForm.msg);        if(SaveForm.msg!=null)        {        m=SaveForm.msg.substring(SaveForm.msg.length() - 1);        }        val=Integer.parseInt(m);        Log.e("val",m);    if(val>0)    {        Toast toast = Toast.makeText(getApplicationContext(),"Data saved",100);        toast.show();    save=true;        MyService.this.stopSelf();    //      alarm.cancel(pintent);        if(alarm!=null)        {            try{            alarm.cancel(pintent);            }            catch(Exception e)            {                Toast toasdst = Toast.makeText(getApplicationContext(),"Massi",100);                toasdst.show();            }            alarm=null;        }           }    }    catch(Exception e)    {        Toast toast = Toast.makeText(getApplicationContext(),"Data Not saved",100);        toast.show();        ///responseStr = responseStrr;    }}@OverrIDepublic voID onDestroy() {    Toast.makeText(this,"Service Destroyed",Toast.LENGTH_SHORT).show();    super.onDestroy();    if(save)    {        try{             stopSelf();        }        catch(Exception e)        {            Toast.makeText(this,"head Bang",Toast.LENGTH_SHORT).show();            super.onDestroy();        }    }    }    }

我已经设置了alarm.cancle但是它会引发异常,因为警报已经为空

我也试过这个

@OverrIDepublic int onStartCommand(Intent intent,int flags,int startID) {    try{      //  String parameter = intent.getStringExtra("param_name");        if(save){            stopSelf();        }    }catch(Exception ex){    }    return startID;}

但没有任何作品服务一次又一次地开始.
还有一件事,如果我没有关闭应用程序,而不是每件事情完美的警报被取消但是当我关闭应用程序并期望运行它同样的背景时它会一次又一次地开始创建.

请帮忙.

解决方法 在使用时将数据存储到服务器

handler.setText(Formact.sendform,Formact.ListString);

只需保存数据即可

Formact.sendform=null Formact.ListString=null

当这些值为null时,应用程序将抛出一个异常,在catch块中捕获它并使用该行关闭

MyService.alarm.cancel(MyService.pintent);    MyService.this.stopService();

服务不会重新开始.

总结

以上是内存溢出为你收集整理的Android服务一次又一次地创建全部内容,希望文章能够帮你解决Android服务一次又一次地创建所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1125339.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存