0
votes

here is my code, I have given a comment line where the error happens (in the catch exception). Anyone can help? thank you very much. Code :

public class AlarmReceiver extends BroadcastReceiver {

    XMLRPCClient client = null;

    private DBclass db;
    int flagPass = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        // For our recurring task, we'll just display a message
        try{
            // Create an object to represent the server.
            client = new XMLRPCClient(new URL("...server.php"));

            if(intent.getStringExtra("ID").equalsIgnoreCase("ON")){
                try {
                    int flag = 0;
                    try {
                        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                        List<ActivityManager.RunningAppProcessInfo> task = am.getRunningAppProcesses();
                        for (int i = 0; i < task.size(); i++) {
                            if (task.get(i).processName.equalsIgnoreCase("com.example.test")) {
                                flag = 1;
                                break;
                            }
                        }
                    }catch (Exception ex){
                        flag = 0;
                    }

                    if(flag == 0){
                        HashMap<String, String> params = new HashMap<String, String>();
                        params.put("COMMAND", "ACON");

                        try {
                            // update buffer
                            client.call("updateDataCommand", params);
                        } catch (XMLRPCException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                catch (Exception ex) {
                    // error here, always catched here
                    flagPass = 1;
                }

                if(flagPass == 1){
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("COMMAND", "ACON");

                    // program error here (apps forced close), unfortunately...
                    try {
                        // update buffer
                        client.call("updateDataCommand", params);
                    } catch (XMLRPCException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

and here is the error message : java.lang.RuntimeException: Unable to start receiver android.os.NetworkOnMainThreadException at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732) .... at android.app.ActivityThread.main(ActivityThread.java:5417) ... Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) at java.net.InetAddress.lookupHostByName(InetAddress.java:431) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)

1

1 Answers

0
votes

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask or Background Thread

@Override
public void onReceive(Context context, Intent intent) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            //put your code here
        }
    }).start();
}