FM搜台时如何选择性保存 搜到的电台
时间:10-02
整理:3721RD
点击:
[SOLUTION]
1.修改FMRaudioActivity.java:
添加:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Dialog;
在FMRadioActivity中添加:
static boolean[] channelsel=null;
private final BroADCastReceiver FMRadioSaveChannelBroadcastReceiver = new
BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
dismissSearchDialog();
int[] chnls=intent.getIntArrayExtra("channels");
LogUtils.d(TAG, ">>>ffhh FMRadioSaveChannelBroadcastReceiver.onReceive " +
Arrays.toString(chnls));
LogUtils.d(TAG, ">>>ffhh onReceive " + intent.getAction());
CharSequence[] chr=new CharSequence[chnls.length];
//if(channelsel==null)
channelsel=new boolean[chnls.length];
for(int i=0;i<chnls.length;i++){
Float ch=new Float(chnls/10.0f);
channelsel=true;
chr = ch.toString() +"Mhz";
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Save channel or not?");
//builder.setMessageset(Arrays.toString(chnls));
builder.setMultiChoiceItEMS(chr,channelsel,
new DialogInterface.OnMultiChoiceCLIckListener() {
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
channelsel[which]=isChecked;
LogUtils.d(TAG, "ffhh select channels "+ which +" checked=" + isChecked);
}
}
);
builder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//int[] channels = null;
if(mService != null) mService.saveFMchannel();
LogUtils.d(TAG, "ffhhsave channels");
}
}
);
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LogUtils.d(TAG, "ffhh do not save channels");
}
}
);
builder.create();
builder.show();
}
};
修改 public void onCreate(Bundle savedInstanceState)
在最后添加:
IntentFiLTEr iFilter = new IntentFilter();
iFilter.adDACtion(FMRadioService.ACTION_SEARCH_FINISHED);
registerReceiver(FMRadioSaveChannelBroadcastReceiver, iFilter);
修改 public void onDestroy()
添加:unregisterReceiver(FMRadioSaveChannelBroadcastReceiver);
2.修改FMRadioService.Java
添加
static final String ACTION_SEARCH_FINISHED =
"android.intent.action.ACTION_SEARCH_FINISHED";
int[] channels = null;
void saveFMchannel() {
int[] result = null;
boolean isScan = true;
result = insertSearchedStation(channels);
tuneStation(FMRadioUtils.computeFrequency(result[0]));
// enableFMAudio(true);
resumeFMAudio();
setMute(false);
LogUtils.d(TAG, "ffhh saveFMchannel" + channels[0]);
Bundle bundle = new Bundle(4);
bundle.putInt(FMRadioListener.CALLBACK_FLAG, FMRadioListener.MSGID_SCAN_FINISHED);
bundle.putInt(FMRadioListener.KEY_TUNE_TO_STATION, result[0]);
bundle.putInt(FMRadioListener.KEY_STATION_NUM, result[1]);
bundle.putBoolean(FMRadioListener.KEY_IS_SCAN, isScan);
notifyActivityStateChanged(bundle);
}
修改:
private int[] insertSearchedStation(int[] channels) {
LogUtils.d(TAG, "insertSearchedStation.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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String defaultStationName = getString(R.string.default_station_name);
for (int i = 0; i < size; i++) {
if (FMRadioUtils.isValidStation(channels)&& FMRadioActivity.channelsel==true) {
if (0 == channelNum) {
firstValidChannel = channels;
}
if (!FMRadioStation.isFavoriteStation(mContext, channels)) {
ops.add(ContentProviderOperation.newInsert(Station.CONTENT_URI)
.withValue(Station.COLUMN_STATION_NAME, defaultStationName)
.withValue(Station.COLUMN_STATION_FREQ, channels)
.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, "insertSearchedStation.firstValidChannel:" + firstValidChannel +
",channelNum:" + channelNum);
return (new int[] { firstValidChannel, channelNum });
}
修改子类FmRadioServiceHandler中handleMessage()
case FMRadioListener.MSGID_SCAN_FINISHED:
将 int[] channels = null;注释掉
下:
else{
中内容全部替换成:
Intent it1= new Intent();
it1.setAction(ACTION_SEARCH_FINISHED);
it1.putExtra("channels",channels);
sendBroadcast(it1);
}
点击cancel键则不保存,直接退出,Thanks!
1.修改FMRaudioActivity.java:
添加:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Dialog;
在FMRadioActivity中添加:
static boolean[] channelsel=null;
private final BroADCastReceiver FMRadioSaveChannelBroadcastReceiver = new
BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
dismissSearchDialog();
int[] chnls=intent.getIntArrayExtra("channels");
LogUtils.d(TAG, ">>>ffhh FMRadioSaveChannelBroadcastReceiver.onReceive " +
Arrays.toString(chnls));
LogUtils.d(TAG, ">>>ffhh onReceive " + intent.getAction());
CharSequence[] chr=new CharSequence[chnls.length];
//if(channelsel==null)
channelsel=new boolean[chnls.length];
for(int i=0;i<chnls.length;i++){
Float ch=new Float(chnls/10.0f);
channelsel=true;
chr = ch.toString() +"Mhz";
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Save channel or not?");
//builder.setMessageset(Arrays.toString(chnls));
builder.setMultiChoiceItEMS(chr,channelsel,
new DialogInterface.OnMultiChoiceCLIckListener() {
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
channelsel[which]=isChecked;
LogUtils.d(TAG, "ffhh select channels "+ which +" checked=" + isChecked);
}
}
);
builder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//int[] channels = null;
if(mService != null) mService.saveFMchannel();
LogUtils.d(TAG, "ffhhsave channels");
}
}
);
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LogUtils.d(TAG, "ffhh do not save channels");
}
}
);
builder.create();
builder.show();
}
};
修改 public void onCreate(Bundle savedInstanceState)
在最后添加:
IntentFiLTEr iFilter = new IntentFilter();
iFilter.adDACtion(FMRadioService.ACTION_SEARCH_FINISHED);
registerReceiver(FMRadioSaveChannelBroadcastReceiver, iFilter);
修改 public void onDestroy()
添加:unregisterReceiver(FMRadioSaveChannelBroadcastReceiver);
2.修改FMRadioService.Java
添加
static final String ACTION_SEARCH_FINISHED =
"android.intent.action.ACTION_SEARCH_FINISHED";
int[] channels = null;
void saveFMchannel() {
int[] result = null;
boolean isScan = true;
result = insertSearchedStation(channels);
tuneStation(FMRadioUtils.computeFrequency(result[0]));
// enableFMAudio(true);
resumeFMAudio();
setMute(false);
LogUtils.d(TAG, "ffhh saveFMchannel" + channels[0]);
Bundle bundle = new Bundle(4);
bundle.putInt(FMRadioListener.CALLBACK_FLAG, FMRadioListener.MSGID_SCAN_FINISHED);
bundle.putInt(FMRadioListener.KEY_TUNE_TO_STATION, result[0]);
bundle.putInt(FMRadioListener.KEY_STATION_NUM, result[1]);
bundle.putBoolean(FMRadioListener.KEY_IS_SCAN, isScan);
notifyActivityStateChanged(bundle);
}
修改:
private int[] insertSearchedStation(int[] channels) {
LogUtils.d(TAG, "insertSearchedStation.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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String defaultStationName = getString(R.string.default_station_name);
for (int i = 0; i < size; i++) {
if (FMRadioUtils.isValidStation(channels)&& FMRadioActivity.channelsel==true) {
if (0 == channelNum) {
firstValidChannel = channels;
}
if (!FMRadioStation.isFavoriteStation(mContext, channels)) {
ops.add(ContentProviderOperation.newInsert(Station.CONTENT_URI)
.withValue(Station.COLUMN_STATION_NAME, defaultStationName)
.withValue(Station.COLUMN_STATION_FREQ, channels)
.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, "insertSearchedStation.firstValidChannel:" + firstValidChannel +
",channelNum:" + channelNum);
return (new int[] { firstValidChannel, channelNum });
}
修改子类FmRadioServiceHandler中handleMessage()
case FMRadioListener.MSGID_SCAN_FINISHED:
将 int[] channels = null;注释掉
下:
else{
中内容全部替换成:
Intent it1= new Intent();
it1.setAction(ACTION_SEARCH_FINISHED);
it1.putExtra("channels",channels);
sendBroadcast(it1);
}
点击cancel键则不保存,直接退出,Thanks!
:):):):):):):):):)
很好,可以不错看看
路过,看看
路过!
