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
复制代码

posted on   南瓜饼  阅读(1105)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
点击右上角即可分享
微信分享提示