我的目标是让UDP数据包接收app从后台启动时连续运行,只要它收到有效数据包就必须处理消息并显示它们。
经过一番研究,我做了以下几点。
我用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 {
@Override
public 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();
}
}
答案 0 :(得分:4)
你还没有开始你的线程。你可以这样做:
Thread initBkgThread = new Thread(new Runnable() {
public void run() {
udp();
}
});
initBkgThread .start();
答案 1 :(得分:3)
这是一个完整的代码还是只是一些剪辑?
首先,您的text1
未初始化。
text1 = findViewById(R.id.<id_of_text_view_in_activity_calls_layout>)
?
答案 2 :(得分:1)
要从您的服务开始活动,您应该创建您的意图:
//Starting Smsgs
Intent startAct = new Intent(context, Smsgs.class);
startAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startAct);
至少需要两个线程,一个用于接收UDP数据包,另一个用于计算数据。你需要修改你的udp()函数:
//Warning, I did not test this code, handle it like pseude-code.
private void udp(){
// ...
// Wait to receive a datagram
dsocket.receive(packet);
Thread showMsg = new Thread(new Runnable() {
public void run() {
// Convert the contents to a string,
String message = new String(buffer, 0, packet.getLength());
Intent intent = new Intent(this, Smsgs.class);
intent.putExtra("msg",message);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
showMsg.start();
// ...
}
也不要忘记启动另一个帖子:initBkgThread.start()