android Web服务广播接收器无法正常工作

时间:2015-10-15 18:57:03

标签: android broadcast

当设备连接可用时,我尝试通过POST请求将数据从我的应用程序SQLite数据库发送到服务器端的MySQL数据库。 我有连接更改BroadcastReceiver作为我的Activity中的内部类,但它不起作用,永远不会被触发。

public class NetworkChangeReceiver extends BroadcastReceiver{

    public static final String url ="http://path/Insert.php";

    @Override
    public void onReceive(final Context context, final Intent intent) {
        // TODO Auto-generated method stub


        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        boolean isConnected = wifi != null && wifi.isConnectedOrConnecting() ||
                mobile != null && mobile.isConnectedOrConnecting();
        if (isConnected) {

        Sql = new SqlCommands(MainActivity.this);
        final Cursor c = Sql.GetData();
        if (c.moveToFirst())
        {
            do {

                try{
                    DefaultHttpClient defaultClient = new DefaultHttpClient();
                    HttpPost post = new HttpPost(url);
                    ArrayList<NameValuePair> List = new ArrayList<NameValuePair>();
                    List.add(new BasicNameValuePair("Latt", c.getString(1)));
                    List.add(new BasicNameValuePair("Lang", c.getString(2)));
                    List.add(new BasicNameValuePair("Date", c.getString(3)));
                    List.add(new BasicNameValuePair("Time", c.getString(4)));

                    post.setEntity(new UrlEncodedFormEntity(List, "UTF-8"));
                    HttpResponse response = defaultClient.execute(post);
                    StatusLine line = response.getStatusLine();
                    if (line.getStatusCode()==HttpStatus.SC_OK)
                    {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        String feedback = out.toString();
                        Log.i("the response", feedback);
                    }
                    }       
                 catch (Exception e) {
                    // TODO: handle exception
                    Log.i("Exception",e.toString());
                }

            } while (c.moveToNext());
        }
        Sql.close();

        //}
        }
     else {
            Log.d("Network Available ", "NO");
        }
    }}}

并在此处注册清单文件:

  <receiver android:name="com.example.demo_gps.MainActivity$NetworkChangeReceiver"  >
        <intent-filter >
            <action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"/>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />

        </intent-filter>
    </receiver>

在应用程序标签上,请提前告知我问题在哪里。

这是数据库代码:

@Override
    protected Boolean doInBackground(Boolean... params) {
        // TODO Auto-generated method stub
           while(MainActivity.this.gotLocation() == false){
               //do nothing, just wait
       }

                     //once done return true
            try {

                    Sql = new SqlCommands(MainActivity.this);
                    Sql.AddEntry(String.valueOf(latitude), String.valueOf(longitude), D, T);

                    runOnUiThread(new Runnable() {
                        public void run() {
                        Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
                        } });                                   

            } catch (Exception e) {
                // TODO: handle exception
                Log.i("Exception",e.toString());
            }

    return true;    

    }
}

0 个答案:

没有答案