java– 通过后台服务启动Android的活动

java– 通过后台服务启动Android的活动,第1张

概述我的目标是让UDP数据包接收应用程序从后台启动时连续运行,当它收到有效数据包时,它必须处理消息并显示它们.经过一番研究,我做了以下几点.>BroadcastReceiver类–在启动时启动服务UP(mstart.java).>用于监视UDP数据包的服务类(udp.java).>显示类以将消息显示为文本(Rmsgs.ja

我的目标是让UDP数据包接收应用程序从后台启动时连续运行,当它收到有效数据包时,它必须处理消息并显示它们.

经过一番研究,我做了以下几点.

> broadcast Receiver类 – 在启动时启动服务UP(mstart.java).
>用于监视UDP数据包的服务类(udp.java).
>显示类以将消息显示为文本(Rmsgs.java).
> GlobalState.Java for Global变量.

我写了一个独立的UDP应用程序与列表视图,它工作正常.因此,我知道这没有问题.

当我编译运行代码服务启动时启动然后崩溃.为了调试,我已经拿走了UDP数据包接收部分.接收到数据包后的UDP类将生成两个数组列表,并将其保存在Global类中,display类将获取它.

    This code is working Now, I found mistake I have made and corrected it.

现在我必须修改以接收udp数据包.

AndroIDManifest.xml中

<?xml version="1.0" enCoding="utf-8"?>
<uses-permission androID:name="androID.permission.WAKE_LOCK"/><uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission androID:name="androID.permission.QUICKBOOT_POWERON"/><application    androID:name="com.mmm.rmsg.GlobalState"    androID:allowBackup="true"    androID:icon="@mipmap/ic_launcher"    androID:label="@string/app_name"    androID:largeHeap="true"    androID:theme="@style/Apptheme" >    <activity        androID:name=".MsgVIEw"        androID:label="@string/app_name" >        <intent-filter>            <action androID:name="androID.intent.action.MAIN" />           <category androID:name="androID.intent.category.LAUNCHER" />        </intent-filter>    </activity>    <receiver androID:enabled="true" androID:exported="true"        androID:name=".mstart"        androID:permission="androID.permission.RECEIVE_BOOT_COMPLETED">        <intent-filter>            <action androID:name="androID.intent.action.BOOT_COMPLETED"/>            <action androID:name="androID.intent.action.QUICKBOOT_POWERON"/>            <category androID:name="androID.intent.category.DEFAulT" />        </intent-filter>    </receiver>    <service androID:name=".udp"      />    </application><uses-permission androID:name="androID.permission.INTERNET"/>

广播接收器类

 package com.mmm.rmsg; import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.Widget.Toast;public class mstart extends broadcastReceiver {@OverrIDepublic voID onReceive(Context context, Intent intent) {    Toast.makeText(context, "Intent detcted.", Toast.LENGTH_LONG).show();    Intent pushIntent = new Intent(context, udp.class);    context.startService(pushIntent);  }  }

服务类

  package com.mmm.rmsg;  import androID.app.Service;  import androID.content.Context;  import androID.content.Intent;  import androID.os.IBinder;  import androID.os.PowerManager;  import androID.Widget.Toast;  import java.util.ArrayList;  import static androID.os.PowerManager.PARTIAL_WAKE_LOCK;  public class udp extends Service {  private static final String LOG_TAG =udp.class.getSimplename();  GlobalState gs = (GlobalState)getApplication(); @OverrIDe public IBinder onBind(Intent arg0){    return  null;}@OverrIDe public int onStartCommand(Intent intent, int flags, int startID) {    setWakeLock();    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();    new Thread(new Server()).start();   return START_STICKY;}private voID setWakeLock(){    PowerManager.WakeLock mWakeLock;    PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);    mWakeLock=powerManager.newWakeLock(PARTIAL_WAKE_LOCK, LOG_TAG);}public class Server implements Runnable {    @OverrIDe    public voID run() {        ArrayList<String> List = new ArrayList<>();        ArrayList<String> cList = new ArrayList<>();        // here udp packets are recvd & processed into 2 List arrays        List.add(0, "MAIN FAIL");        List.add(1,"BOILER HEATER 20C");        List.add(2, "COOliNG NEED ATT");        cList.add(0, "6");        cList.add(1,"1");        cList.add(2, "5");        GlobalState gs = (GlobalState)getApplication();          gs.setGmList(List);          gs.setGcList(cList);        call();    }}public voID call() {    Intent dialogIntent = new Intent(getBaseContext(), MsgVIEw.class);    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    startActivity(dialogIntent);   }  }

全球课程

   package com.mmm.rmsg;  import java.util.ArrayList;  import androID.app.Application;  public class GlobalState extends Application  {    private ArrayList<String> GmList = new ArrayList<>();    private ArrayList<String> GcList = new ArrayList<>();    private boolean chk = true;    private boolean cchk = true;   public ArrayList<String> getGmList() {    chk = GmList.isEmpty();    if(chk==true)    {        GmList.add(0,"No Calls");    }     return GmList;  }   public ArrayList<String> getGcList() {      cchk = GcList.isEmpty();    if(cchk==true)    {        GcList.add(0,"0");    }    return GcList;}    public voID setGmList(ArrayList<String> Gmlit) {        for (int i = 0; i < Gmlit.size(); i++) {            this.GmList.add(i, Gmlit.get(i));        }  }  public voID setGcList(ArrayList<String> Gclit) {    for (int i = 0; i < Gclit.size(); i++) {        this.GmList.add(i, Gclit.get(i));      }     }   }

显示类

    package com.mmm.rmsg;    import androID.support.v7.app.AppCompatActivity;    import androID.os.Bundle;    import androID.vIEw.Menu;    import androID.vIEw.MenuItem;    import androID.Widget.ListVIEw;    import androID.content.Context;    import androID.graphics.color;    import androID.Widget.ArrayAdapter;    import androID.vIEw.VIEw;    import androID.vIEw.VIEwGroup;    import androID.Widget.TextVIEw;    import java.util.ArrayList;    import java.util.Arrays;    public class MsgVIEw extends AppCompatActivity {        ListVIEw ListVIEw ;        ArrayList<String> mList = new ArrayList<>();        ArrayList<String> pList = new ArrayList<>();        @OverrIDe        protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_msg_vIEw);            // Get ListVIEw object from xml            ListVIEw = (ListVIEw) findVIEwByID(R.ID.List);            GlobalState gs = (GlobalState) getApplication();                mList= gs.getGmList();                pList= gs.getGcList();                String[] msgArray = mList.toArray(new String[mList.size()]);                Arrays.toString(msgArray);                String[] clrArray = pList.toArray(new String[pList.size()]);                Arrays.toString(clrArray);                ListVIEw.setAdapter(new colorArrayAdapter(this, androID.R.layout.simple_List_item_1, msgArray,clrArray));        }        public class colorArrayAdapter extends ArrayAdapter<Object>{            private String[] List;            private String[] p;            public colorArrayAdapter(Context context, int textVIEwResourceID,                             Object[] objects, Object[] obj) {            super(context, textVIEwResourceID, objects);            List = new String[objects.length];            for (int i = 0; i < List.length; i++)                List[i] = (String) objects[i];            p = new String[objects.length];                for (int i = 0; i < p.length; i++)                p[i] = (String) obj[i];    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        VIEw vIEw = (TextVIEw)super.getVIEw(position, convertVIEw, parent);        String c;        for(int x=0; x< List.length; x++)        {            c=chk(x,p);            if("R".equals(c) && position==x ) {                vIEw.setBackgroundcolor(color.RED);            }            else            if("Y".equals(c) && position==x) {                vIEw.setBackgroundcolor(color.YELLOW);            }            else            if("G".equals(c) && position==x) {                vIEw.setBackgroundcolor(color.GREEN);           }        }            return vIEw;        }    }        public  String chk(int IDx, String[] table){    String res;    if("6".equals(table[IDx]) || "7".equals(table[IDx])  || "8".equals(table[IDx])) {        res = "R";    }    else    if("4".equals(table[IDx])  || "5".equals(table[IDx])) {        res = "Y";    }    else    if("1".equals(table[IDx])|| "2".equals(table[IDx]) || "3".equals(table[IDx]) ) {        res = "G";    }    else{        res = "W";    }        return res;    }        @OverrIDe        public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_msg_vIEw, menu);        return true;    }        @OverrIDe        public boolean onoptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will            // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroIDManifest.xml.        int ID = item.getItemID();        //noinspection SimplifiableIfStatement        if (ID == R.ID.action_settings) {            return true;            }    return super.onoptionsItemSelected(item);        }        @OverrIDe        protected voID onDestroy(){            super.onDestroy();        }    }

解决方法:

你还没有开始你的线程.你可以这样做:

Thread initBkgThread = new Thread(new Runnable() {        public voID run() {            udp();        }    });initBkgThread .start();
总结

以上是内存溢出为你收集整理的java – 通过后台服务启动Android的活动全部内容,希望文章能够帮你解决java – 通过后台服务启动Android的活动所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存