20189210 移动平台开发第十二周作业

第47章服务

服务(service)是一种Android组件。服务没有用户界面,并且在后台运行。它适用于长时间运行的操作。

  • 服务和在其中声明服务的应用程序在相同的进程上运行,并且在应用程序的主线程上运行。
  • 服务可以采取两种形式之一,它可以是启动的或绑定的。
    如果一个组件启动了服务,它就是启动的。即便启动服务的组件已经不再接受服务或者被销毁了,该服务还可以在后台无限期地运行。
    如果应用程序组件绑定到服务,该服务就是绑定的。绑定的服务就像是客户端-服务器关系中的一台服务器,接受来自其他应用程序组件的请求,并且返回结果。
  • 术语可访问性(accessibility),表示一个服务可以是私有的或公有的。公有的服务器可以由任何的应用程序调用,私有的服务器只能够由服务声明所在的相同的应用程序之中的组件来访问。
  • 服务必须在清单中的之下使用service元素来声明。

第48章广播接收器

Android系统总是会将在操作系统和应用程序运行期间发生的意图进行广播。此外,应用程序也可以广播用户定义的意图,可以通过在应用程序中编写广播接收器来利用这些广播。

  • 广播接收器是一个应用程序组件,它监听一个特定意图广播,类似于监听事件的Java监听器。
  • 基于时钟的广播接收器

第49章 闹钟服务

相关方法:

set(int type,long startTime,PendingIntent pi):一次性闹钟
setRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,和3有区别,3闹钟间隔时间不固定
setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi): 重复性闹钟,时间不固定
cancel(PendingIntent pi):取消AlarmManager的定时服务
getNextAlarmClock():得到下一个闹钟,返回值AlarmManager.AlarmClockInfo
setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) 和set方法类似,这个闹钟运行在系统处于低电模式时有效
setExact(int type, long triggerAtMillis, PendingIntent operation): 在规定的时间精确的执行闹钟,比set方法设置的精度更高
setTime(long millis):设置系统墙上的时间
setTimeZone(String timeZone):设置系统持续的默认时区
setWindow(int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation): 设置一个闹钟在给定的时间窗触发。类似于set,该方法允许应用程序精确地控制操作系统调 整闹钟触发时间的程度。

  • 代码示例
public class WakeUpActivity extends Activity {
    private final int NOTIFICATION_ID = 1004;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = getWindow();
        Log.d("wakeup", "called. oncreate");
        window.addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.activity_wake_up);
        addNotification();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void dismiss(View view) {
        NotificationManager notificationMgr = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        notificationMgr.cancel(NOTIFICATION_ID);
        this.finish();
    }

    private void addNotification() {
        NotificationManager notificationMgr = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        Notification notification  = new Notification.Builder(this)
                .setContentTitle("Wake up")
                .setSmallIcon(android.R.drawable.star_on)
                .setAutoCancel(false)
                .build();
        notification.defaults|= Notification.DEFAULT_SOUND;
        notification.defaults|= Notification.DEFAULT_LIGHTS;
        notification.defaults|= Notification.DEFAULT_VIBRATE;
        notification.flags |= Notification.FLAG_INSISTENT;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationMgr.notify(NOTIFICATION_ID, notification);
    }
}

第50章内容提供者

内容提供者是用来封装要和其他应用程序共享的数据的一个Android组件。

  • 要创建一个内容提供者,你需要扩展android.content.ContentProvider类,这个类提供CRUD方法,也就是用于创建、访问、更新和删除数据的方法。
  • 内容提供者中的数据,通过一个独特的URI来引用。内容提供者的消费者,必须知道这个URL,才能够访问内容提供者的数据。
  • ContentProvider类
  • query方法(访问底层的数据)
  • insert方法(添加一个数据项)
  • update方法(可以使用u方法来更新一个数据项或一组数据项)
  • delete方法(删除一个数据项或一组数据项)
  • 创建一个内容提供者
  • 消费内容提供者
原文地址:https://www.cnblogs.com/20189210mujian/p/10878923.html