Android学习——后台程序

摘要:
Android学习-背景程序在Android系统中,我们一直在接触前台界面程序。事实上,我们从接触安卓系统开始就听说过它们。有两种程序:有接口和无接口。实现代码:StringsvcName=Context。通知服务;通知管理器通知管理器;notificationManager=getSystemService;//选择adrawabletodisplay as statusbricannticon=R.drawable.icon;//通知启动时状态栏中显示的文本StringtickerText=“通知”;//当=System.currentTimeMillis()时,扩展状态错误通知intimeorderlongwhen;Notificationnotification=newNotification;Contextcontext=getApplicationContext();//要在扩展状态窗口StringexpandedText=“扩展状态文本”中显示的文本;//TitleforthexpandedstatusStringexpandedTitle=“NotificationTitle”;//当单击扩展文本时Intentlaunchactivity Intent=newIntent;PendingIntentlaunchIntent=PendingIntent获取活动;notification.setLatestEventInfo;触发器方法:intnotificationRef=1;notificationManager.notify;学习“活动”后,编写背景程序并不难!!

Android学习——后台程序

Android系统中我们一直在接触着前台界面程序,其实在一开始接触Android时就听说了,程序就有有界面和无界面之分。后台程序就是这类无界面的程序,它在后台执行,没有影响你的界面。比如短信监听程序,执行在后台,当有短信时才给你们提示,振动或声音;比如闹钟,设定好时间后,在定时通知你;再比如mp3播放器,选择好音乐后,在待在后台唱着,当有电话来时,自动暂停,完后再继续播放。

其实分析下来,我们不难发现,后台程序跟前台程序是一样的,也就是在执行我们指定的程序,只是留给我们两个问题,.因为没有界面,我们会问,怎么启动,怎么终止?2.因为没有界面,这程序如何通知我们一些信息或状态

前面的学习让我们知道,一个ActivityCall另一个Activity时,只需要能过中介人Intent就可以了,同样我们与服务处理类打交道也是通过Intent来实现,当然,界面类是继承着Activity,而服务类则是继承着Service类。

启动服务:

  // Implicitly start a Service

startService(new Intent(MyService.MY_ACTION));

// Explicitly start a Service

startService(new Intent(this, MyService.class));

       停止服务:

              stopService(new Intent(this, MyService.class));

 

       同样,跟Activity一样的生命期中,系统也会自动跟据不同的状态来调用继承函数:

@Override

public void onCreate()

public IBinder onBind(Intent intent)

public void onStart(Intent intent, int startId)

。。。

       在实际的开发中,我们一般都不会直接写一个服务类,一般都会写一个与后台程序相配套的前台程序,一般的程序总会有一些配置吧~~,然后这个界面中就可以很方便地来控制后台程序的运作。

 

 

       我们来回答第二个问题,就是在服务中我们怎么发起一个通知给用户,在Andorid中,提供了以下几种方式:

1.      Toast

这是一个无模式的小窗体,会将显示的信息显示在首页面中:

Android学习——后台程序第1张

实现代码是:

Context context = getApplicationContext();

String msg = To the bride an groom!;

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, msg, duration);

int offsetX = 0;

int offsetY = 0;

toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);

toast.show();

当然,你也可以显示更杂的,可以将一个控制直接当成一个Toast显示出来,也可以自定义一个控件显示出来,自定义控件的强大是大家都知道的~~

 

2.      Notifications

这种方式是系统中比较通用的模式,通过这种方式你可以使系统:将一个图标在状态条上闪,让机器震动,发出声音等。

实现代码:

String svcName = Context.NOTIFICATION_SERVICE;

NotificationManager notificationManager;

notificationManager = (NotificationManager)getSystemService(svcName);

// Choose a drawable to display as the status bar icon

int icon = R.drawable.icon;

// Text to display in the status bar when the notification is launched

String tickerText = Notification;

// The extended status bar orders notification in time order

long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();

// Text to display in the extended status window

String expandedText = Extended status text;

// Title for the expanded status

String expandedTitle = Notification Title;

// Intent to launch an activity when the extended text is clicked

Intent intent = new Intent(this, MyActivity.class);

PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);

notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);

       触发方式:

int notificationRef = 1;

notificationManager.notify(notificationRef, notification);

 

学会了Activity再写个后台程序也就不难了!!

这里顺便再提一下,在Android系统中也提供了多线程编程,我们知道不管是前台还是后台程序,都有生命期的,当程序不活动时,我们想继续让程序执行,这里我们需要用到线程了,在Android系统中使用线程,跟我们直接写java线程程序非常想似:

// This method is called on the main GUI thread.

private void mainProcessing() {

// 主程序中启动线程.

Thread thread = new Thread(null, doBackgroundThreadProcessing, Background);

thread.start();

}

// Runnable that executes the background processing method.

private Runnable doBackgroundThreadProcessing = new Runnable() {

public void run() {

//线程执行内容。。。

}

};

 

免责声明:文章转载自《Android学习——后台程序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Kafka长文总结FileStream下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android Studio快捷键——编辑篇

Android Studio是官方推出的Android开发IDE,本系列讲解Android Studio中常用的快捷键,本文是该系列的第一篇,讲解的内容是与编辑代码相关的快捷键。 本文所讲快捷键基于Android Studio2.3.3 windows版本。 本文所记录的快捷键皆亲自实践,全部可用。 编辑 编辑是IDE的核心功能,Android Studi...

windows api线程

一、1、定义入口函数static void threadFunc(void);//在TestDlg.h里面声明 void CTestDlg::threadFunc(void)     //在TestDlg.cpp里定义 {   Sleep(1000);   AfxMessageBox(_T("OK!"));} 2、定义线程句柄:HANDLE hthread...

Linux获取线程tid线程名

Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned long)chThreadName); //tid syscall(SYS_gettid)...

uiautomatorviewer详解

一,uiautomatorviewer是什么?     Android 4.1发布的,uiautomator是用来做UI测试的。也就是普通的手工测试,点击每个控件元素 看看输出的结果是否符合预期。比如 登陆界面 分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。 功能性或者黑盒UI测试不需要测试人员了解程序如何实现的,只需...

Windows I/O完成端口

内容: 1、基本概念2、WINDOWS完成端口的特点3、完成端口(Completion Ports )相关数据结构和创建4、完成端口线程的工作原理5、Windows完成端口的实例代码 WINDOWS完成端口编程 摘要:开发网络程序从来都不是一件容易的事情,尽管只需要遵守很少的一些规则:创建socket,发起连接,接受连接,发送和接收数据,等等。真正的困难在...

深入理解JVM虚拟机3:垃圾回收器详解

本文转自:https://www.cnblogs.com/snailclimb/p/9086341.html 本系列文章将整理到我在GitHub上的《Java面试指南》仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下Star哈 文章将同步到我的个人博客: www.how2pl...