zoukankan      html  css  js  c++  java
  • Android学习(七)

    学号 20189214 《Java程序设计》第十二周学习总结

    服务

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

    广播接收器

    Android系统总是会将在操作系统和应用程序运行期间发生的意图进行广播。此外,应用程序也可以广播用户定义的意图,可以通过在应用程序中编写广播接收器来利用这些广播。
    广播接收器是一个应用程序组件,它监听一个特定意图广播,类似于监听事件的Java监听器。

    闹钟服务

    相关方法:

    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);
        }
    }
    

    内容提供者

    内容提供者是用来封装要和其他应用程序共享的数据的一个Android组件。
    要创建一个内容提供者,你需要扩展android.content.ContentProvider类,这个类提供CRUD方法,也就是用于创建、访问、更新和删除数据的方法。
    内容提供者中的数据,通过一个独特的URI来引用。内容提供者的消费者,必须知道这个URL,才能够访问内容提供者的数据。
    ContentProvider类
    query方法(访问底层的数据)
    insert方法(添加一个数据项)
    update方法(可以使用u方法来更新一个数据项或一组数据项)
    delete方法(删除一个数据项或一组数据项)

    在音乐播放器中使用了query来找到数据库中音乐。

     long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//音乐id
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//音乐标题
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑
                long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id
                long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
                long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//文件大小
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//文件路径
                int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
    

    学习进度条

    代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
    目标 5000行 30篇 400小时
    第一周 150/200 2/2 20/20
    第二周 200/400 1/3 20/40
    第三周 100/500 1/4 10/50
    第四周 200/700 1/5 15/65
    第五周 1486/2186 1/6 15/80
    第六周 1400/3586 1/7 18/98
    第七周 1400/5000 1/8 18/116
    第八周 1200/6200 1/9 15/131
    第九周 800/7000 2/11 12/143
    第十周 1500/8500 1/12 15/158
    第十一周 1500/10000 1/13 10/168
    第十二周 1200/11200 1/14 12/180
    • 计划学习时间:15小时

    • 实际学习时间:12小时

    参考资料

  • 相关阅读:
    近期需要学习的
    sdfsdf
    思路绝对清楚,手段绝对下流【转】
    一宅镇京华——看谁还敢在我面前吹丫有钱 
    白天求生存,晚上求发展
    Ten Places to Go for SharePoint Development Information
    Life Record
    Custom Search Result Style Based On SharePoint Xslt Search Result Style
    软考信息系统项目管理师考试记录
    什么是Microsoft SharePoint 2010
  • 原文地址:https://www.cnblogs.com/Shambryce/p/10887414.html
Copyright © 2011-2022 走看看