android的Notification學習

來源:互聯網
上載者:User

activity代碼

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代碼:

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代碼:

package com.example.studyreceiver;import android.app.Notification;import android.app.NotificationManager;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 {    protected Context mContext;    @Override    public void onReceive(Context context, Intent intent) {        mContext = context;        method1();        // method2(intent);    }    private void method1() {        // 第一種        ConnectivityManager connectivityManager = (ConnectivityManager) mContext                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo MOBILENetInfo = connectivityManager                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        NetworkInfo WIFINetInfo = connectivityManager                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);        if (!MOBILENetInfo.isConnected() && !WIFINetInfo.isConnected()) {            Toast.makeText(mContext, "沒用可用的網路 嘿嘿", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(mContext, "有能用的網路呀", Toast.LENGTH_SHORT).show();            Notification notification = new Notification(                    R.drawable.ic_launcher, "Notification測試",                    System.currentTimeMillis());            NotificationManager notificationManager = (NotificationManager) 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, "測試", "有可用的網路呀",                    pendingIntent02);            notificationManager.notify(0, notification);        }    }}

 

代碼下載:http://download.csdn.net/detail/wenwei19861106/4568754

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.