分类:C#、Android、VS2015;

创建日期:2016-03-01

一、简介

这一节演示带Intent过滤器的Services的基本用法。

1、配置Intent Filter

不论是本地解决方案还是远程解决方案,都可以通过intent过滤器显式指定要调用的是哪个已启动的服务。换言之,Intent过滤器主要解决的问题是:在Activity中调用StartService方法启动自定义的Service类以后,通过Intent过滤器指定调用的是已启动的服务中的哪些服务(过滤掉其他的服务)。当然,其他应用程序也可以用这种方式来实现。

在C#代码中,可以用[IntentFilte(…)]特性声明来自动注册Intent过滤器,其作用就像可以用[Service]特性声明自动注册服务一样,也是让系统自动修改AndroidManifest.xml配置文件,而不需要我们去手工在AndroidManifest.xml文件中注册配置信息。

例如,下面的代码将Intent过滤器与 mj.demos.DemoService 关联在一起:

namespace ServiceDemo1
{
[Service]
[IntentFilter(new String[]{"mj.demos.ServiceDemo1"})]
public class MyService : Service
{
……
}
}

通过C#代码声明Service特性和IntentFilter特性以后,系统就会自动在AndroidManifest.xml文件中添加下面的配置:

<service android:name="servicedemo1.MyService">
<intent-filter>
<action android:name="mj.demos.MyService" />
</intent-filter>
</service>

其中,“servicedemo1.MyService”中的servicedemo1是将命名空间ServiceDemo1全部变为小写字母后的结果,MyService是自定义的服务名。

这样一来,就可以在任何类中调用StartService方法,并在参数中传递Intent过滤器。例如,下面的代码演示了如何在某个Activity中传递Intent过滤器:

StartService (new Intent ("mj.demos.MyService"));

下面的代码演示了如何停止过滤服务:

StopService (new Intent ("mj.demos.MyService"));

2、通知用户

利用前面章节介绍的通知用户的办法,可将后台运行的Service的状态或结果告诉用户,比如传输一个大文件时,告诉用户后台传输已完成等。

二、示例2--StartedServiceDemo2

该例子与上一节介绍的StartedServiceDemo1的运行结果相同。不同的是在这个例子中添加了Intent过滤器。

运行截图

  

主要设计步骤

(1)添加ch1602_Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/ch1602StartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动服务" />
<Button
android:id="@+id/ch1602StopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
</LinearLayout>

(2)添加ch1602ServiceDemo.cs

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using System.Threading; namespace MyDemos.SrcDemos
{
[Service]
[IntentFilter(new string[] { action })]
public class ch1602ServiceDemo : Service
{
public const string action = "MyDemos.ch1602Service";
Thread thread; [return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
var myHandler = new Handler(MainLooper);
myHandler.Post(() =>
{
Toast.MakeText(this, "服务已启动", ToastLength.Short).Show();
});
thread = new Thread(() =>
{
for (int i = 1; i <= 10; i++)
{
var msg = string.Format("这是来自服务的第{0}个消息", i);
Thread.Sleep(TimeSpan.FromSeconds(4));
myHandler.Post(() =>
{
Toast.MakeText(this, msg, ToastLength.Short).Show();
});
}
StopSelf();
});
thread.Start();
return StartCommandResult.Sticky;
} public override void OnDestroy()
{
base.OnDestroy();
thread.Abort();
new Handler(MainLooper).Post(() =>
{
Toast.MakeText(this, "服务已停止", ToastLength.Short).Show();
});
} //基类要求实现的接口
public override IBinder OnBind(Intent intent)
{
return null;
}
}
}

(3)添加ch1602MainActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "ch1602MainActivity")]
public class ch1602MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1602_Main); var start = FindViewById<Button>(Resource.Id.ch1602StartService);
start.Click += delegate
{
StartService(new Intent(ch1602ServiceDemo.action));
}; var stop = FindViewById<Button>(Resource.Id.ch1602StopService);
stop.Click += delegate
{
StopService(new Intent(ch1602ServiceDemo.action));
};
}
}
}

【Android】16.3 带Intent过滤器的Services的更多相关文章

  1. Android开发-API指南-Intent和Intent过滤器

    Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...

  2. Android——Intent和Intent过滤器

    http://www.cnblogs.com/XP-Lee/p/3613830.html Intent就是一个激活组件的消息对象,用于组件之间的通信.需要注意的是,能被Intent激活通信的组件只有三 ...

  3. Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习

    16.7挑战练习:优化照片显示 新建dialog_photo.xml 1234567891011121314 <?xml version="1.0" encoding=&qu ...

  4. Android组件的通讯——Intent

    转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激 ...

  5. Android应用开发中Intent的作用及使用方法

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  6. 【Android】12.1 Intent基本概念

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 Intent:意图,含义就是你想利用它调用哪个组件实现相关的功能,比如调用相机组件实现拍照.调用Contact组件 ...

  7. Android开发学习之Intent具体解释

    Intent简单介绍和具体解释:           Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述.               ...

  8. 【Android】自带Theme

    [Android]自带Theme android之uses-permission   在编写Android程序时经常会忘记添加权限,下面是网上收集的关于Android uses-permission的 ...

  9. Android SurfaceView实战 带你玩转flabby bird (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...

随机推荐

  1. JSTL详解(一)

    将jstl.jar包导入到工程中 jstldemo1.jsp <%@ taglib prefix="c" uri="http://java.sun.com/jsp/ ...

  2. iOS CGRectGetMaxX/Y 使用

    在iOS的界面布局中我们能够使用CGRectGetMaxX 这种方法来方便的获取当前控件的x坐标值+宽度的数值.这样便能够方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...

  3. 怎样查看class文件的jdk版本号

    1.事先编译好一个class文件.如:TestVersion.class 2.使用UltraEdit或Editplus打开class文件,我这里使用的editplus,如图: 3.打开时Encodin ...

  4. MySQL 联合索引测试3

    接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看 ...

  5. JavaScript toString、String和stringify方法区别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. jQuery实现页面关键词高亮

    示例代码,关键位置做了注释,请查看代码: <html> <head> <title>jQuery实现页面关键词高亮</title> <style ...

  7. ecipse theme

    市场里搜“jeeeyul's Eclipse Themes”

  8. Python pycharm(windows版本)部署spark环境

    一 部署本地spark环境 1.1  安装好JDK       下载并安装好jdk1.7,配置完环境变量.   1.2 Spark环境变量配置       去http://spark.apache.o ...

  9. 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值

    上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...

  10. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...