我的意图服务没有从广播接收器调用

时间:2014-01-24 05:37:36

标签: android broadcastreceiver intentservice

这是我的广播接收器

public class InternetConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            // Do something
            Toast.makeText(
                    context,
                    "wifi" + wifi.isAvailable() + "mobile"
                            + mobile.isAvailable(), Toast.LENGTH_LONG).show();

                context.startService(new Intent(context,
                        DataDownloadService.class));
            }

    }

}

这是我的服务

public class DataDownloadService extends IntentService {
    private static ORM orm = new ORM();

    public DataDownloadService() {
        super("hello");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "Service inside", Toast.LENGTH_LONG).show();

        Bitmap AdsDrawable = null;
        InputStream inputStream = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            URI uri = URI
                    .create(myurlstring);
            HttpGet get = new HttpGet(uri);
            HttpResponse httpResponse = httpclient.execute(get);
            inputStream = httpResponse.getEntity().getContent();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    inputStream));

            String datarow = "", data = "";
            while ((datarow = br.readLine()) != null) {

                data = datarow + "\n";
            }
            Log.w("Chaitanya", data);


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

   //work with data and show notification


NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                // old implementation API level <=10
                Notification notification = new Notification(
                        R.drawable.testing_visaul, "My Notification",
                        System.currentTimeMillis());

                Intent intent1 = new Intent(getApplicationContext(),
                        MainActivity.class);

                PendingIntent pendingIntent = PendingIntent.getActivity(
                        getApplicationContext(), 0, intent1, 0);

                notification.setLatestEventInfo(getBaseContext(),
                        "My Notification Title", "New Event", pendingIntent);

            }
        }
    }
}

清单条目如下

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <receiver
        android:name="InternetConnectivityReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>


    <service android:name="DataDownloadService"></service>

0 个答案:

没有答案