Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 如何內置FM頻道 M

Android 如何內置FM頻道 M

編輯:關於Android編程

 

如有5個頻道需要內置,即燒完軟件後第一次進入FM,還沒有進行搜索時,列表就可以始終顯示內置頻道

 

FMRadioService.java
1.private  int defaultChannels[] = new int[] {875, 903, 918, 942, 1025};//自定義內置頻道
2.initService()中添加insertDefaultStation(defaultChannels);
3.定義insertDefaultStation()函數
 private int[] insertDefaultStation(int[] channels) {
        LogUtils.d(TAG, insertDefaultStation.firstValidChannel: + Arrays.toString(channels));
        int firstValidChannel = mCurrentStation;
        int channelNum = 0;
        if (null != channels) {
            Arrays.sort(channels);
            int size = channels.length;
            // Save searched stations into database by batch
            ArrayList ops = new ArrayList();
            String defaultStationName = getString(R.string.default_station_name);
            for (int i = 0; i < size; i++) {
                if (FMRadioUtils.isValidStation(channels[i])) {
                    if (0 == channelNum) {
                        firstValidChannel = channels[i];
                    }
                    if (!FMRadioStation.isDefaultStation(mContext, channels[i])) {
                        ops.add(ContentProviderOperation.newInsert(Station.CONTENT_URI)
                                .withValue(Station.COLUMN_STATION_NAME, defaultStationName)
                                .withValue(Station.COLUMN_STATION_FREQ, channels[i])
                                .withValue(Station.COLUMN_STATION_TYPE, FMRadioStation.STATION_TYPE_SEARCHED)
                                .build());
                    }
                    channelNum++;
                }
            }
            // Save search stations to database by batch
            try {
                mContext.getContentResolver().applyBatch(FMRadioStation.AUTHORITY, ops);
            } catch (RemoteException e) {
                LogUtils.d(TAG, Exception when applyBatch searched stations  + e);
            } catch (OperationApplicationException e) {
                LogUtils.d(TAG, Exception when applyBatch searched stations  + e);
            }
        }
        LogUtils.d(TAG, insertDefaultStation.firstValidChannel: + firstValidChannel + ,channelNum: + channelNum);
        return (new int[] { firstValidChannel, channelNum });
    }
4.handleMessage()->case FMRadioListener.MSGID_SCAN_FINISHED:中
 result = insertSearchedStation(channels);
 insertDefaultStation(defaultChannels);//添加此行
5.FMRadioStation.java中添加
 public static boolean isDefaultStation(Context context, int iStation) {
        return isStationExist(context, iStation, STATION_TYPE_SEARCHED);
    }
 
 
 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved