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
分类:
android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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语句:使用策略模式优化代码结构