Android notification Learning

Source: Internet
Author: User

Activity Code

package com.example.studyreceiver;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity implements OnClickListener {    NetworkReceiver mReceiver;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // Intent intent = getIntent();        // String data = intent.getStringExtra("data");        if (mReceiver != null) {            // unregisterReceiver(mReceiver);        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.register_receiver:            this.startService(new Intent(this, ServiceDemo.class));            break;        default:            break;        }    }}

Service Code:

package com.example.studyreceiver;import android.app.Service;import android.content.Intent;import android.content.IntentFilter;import android.net.ConnectivityManager;import android.os.IBinder;public class ServiceDemo extends Service {    NetworkReceiver mReceiver;    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();        IntentFilter filter = new IntentFilter(                ConnectivityManager.CONNECTIVITY_ACTION);        NetworkReceiver mReceiver = new NetworkReceiver();        registerReceiver(mReceiver, filter);    }    @Override    public void onDestroy() {        super.onDestroy();        unregisterReceiver(mReceiver);    }}

 

Receiver code:

Package COM. example. studycycler; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android.net. connectivitymanager; import android.net. networkinfo; import android. widget. toast; public class networkreceiver extends broadcastreceiver {protec Ted context mcontext; @ override public void onreceive (context, intent) {mcontext = context; Method1 (); // method2 (intent);} private void Method1 () {// first connectivitymanager = (connectivitymanager) mcontext. getsystemservice (context. connectivity_service); networkinfo mobilenetinfo = connectivitymanager. getnetworkinfo (connectivitymanager. type_mobile); Network Info wifinetinfo = connectivitymanager. getnetworkinfo (connectivitymanager. type_wifi); If (! Mobilenetinfo. isconnected ()&&! Wifinetinfo. isconnected () {toast. maketext (mcontext, "no available network", toast. length_short ). show ();} else {toast. maketext (mcontext, "networks available", toast. length_short ). show (); Notification = new notification (R. drawable. ic_launcher, "Notification test", system. currenttimemillis (); icationicationmanager notifmanager = (icationicationmanager) mcontext. getsystemservice (context. notification_service); intent startintent = new intent (mcontext, mainactivity. class); startintent. putextra ("data", "data"); pendingintent pendingintent02 = pendingintent. getactivity (mcontext, 0, startintent, 0); notification. setlatesteventinfo (mcontext, "test", "available network", pendingintent02); icationicationmanager. Y (0, notification );}}}

 

Code download: http://download.csdn.net/detail/wenwei19861106/4568754

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.