android的Service和Notification学习

在安卓应用中经常看到比如一连接网络 某个应用就在状态栏上更新一些信息

今天花了一个下午研究下贴上代码:

package com.example.study;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = getIntent();
		String data = intent.getStringExtra("data");
		if (data != null && data.equals("data")
				&& ServiceDemo.notificationManager != null) {
			ServiceDemo.notificationManager.cancel(0);
			ServiceDemo.notificationManager=null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public boolean detect(Context con) {

		ConnectivityManager manager = (ConnectivityManager) con
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);

		if (manager == null) {
			return false;
		}

		NetworkInfo networkinfo = manager.getActiveNetworkInfo();

		if (networkinfo == null || !networkinfo.isAvailable()) {
			return false;
		}

		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_service:
			this.startService(new Intent(this, ServiceDemo.class));
			break;

		default:
			break;
		}
	}

}

  service代码:

package com.example.study;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class ServiceDemo extends Service {
    public static NotificationManager notificationManager;
    Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                if (notificationManager == null) {
                    addNotificaction();
                }
                break;
            default:
                break;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000 * 60);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Boolean b = detect(ServiceDemo.this);
                    System.out.println(b);
                    if (b) {
                        Message msg = new Message();
                        msg.what = 1;
                        mHandler.sendMessage(msg);
                    }
                }

            }
        }).start();
    }

    private void addNotificaction() {
        Notification notification = new Notification(R.drawable.ic_launcher,
                "Notification测试", System.currentTimeMillis());
        notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent startintent = new Intent(this, MainActivity.class);
        startintent.putExtra("data", "data");
        PendingIntent pendingIntent02 = PendingIntent.getActivity(this, 0,
                startintent, 0);
        notification.setLatestEventInfo(this, "测试", "跳转到主界面", pendingIntent02);
        notificationManager.notify(0, notification);

    }

    public boolean detect(Context con) {

        ConnectivityManager manager = (ConnectivityManager) con
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);

        if (manager == null) {
            return false;
        }

        NetworkInfo networkinfo = manager.getActiveNetworkInfo();

        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }

        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("CountService", "on destroy");
    }

}
代码下载:http://download.csdn.net/detail/wenwei19861106/4566084
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值