主要知识点:
1. 在非Activity程序中(如service 或者 BroadcastReceiver等)调用StartActivity(),即从外部调用Activity;
Activity 继承了context,并重载了startActivity()方法;不能像内部调用一样,直接使用startActivity,为了保护Activity 堆栈,
此时需要开启一个新的task,加一个flag,
添加: intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 即可;
2. 线程的使用;
创建线程:
public class MyRateThread implements Runnable {
public MyRateThread() {}
public void run() {
mHandler.post(new Runnable(){ //add a new task to the pthread ;
public void run(){
}
});
}
调用线程:
Thread pthread = new Thread(new MyRateThread());
pthread.start();
3. 利用Sharedpreference 读取和存放数据;
获取Sharedpreference 对象;
getSharedPreferences( , );
读取数据:
getSharedPreferences( , ).getValue();
改写数据:
获取edit对象:getSharedPreferences( , ).edit();
更改数据: getSharedPreferences( , ).edit().putvalue();
提交: editor.commit();
The defferences betweengetDefaultSharedPreference() && getSharedPreference()
// With a default name stored withinproject, perhaps in ...sharedpreference/...
SharedPreferences dictSrp = PreferenceManager.getDefaultSharedPreferences(context);
dictSrp.getBoolean(data.fileName, false); (if sharepreference contains data.filename, return true, or false.)
// require a name, like "exDictSetting"
SharedPreferences dictSp = context.getSharedPreferences("exDictSetting", Context.MODE_PRIVATE);
Editor erpEdit = dictSp.edit();
(return a new Editor for the preferences, through which you can make modifications to the data in the preference. And you have to call commite() to have any changes you perform in the editor to show up in this Sharedperference object.)
//before using onSharedPreferenceChanged(), some functions like getDefaultSharedPreference(), unregisterOnSharedPreferenceChangeListener(),
//unregisterReceiver() should be decladed.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.unregisterOnSharedPreferenceChangeListener(this);
unregisterReceiver();
onSharedPreferenceChanged();
4. AlertDialog 更改大小;
Window window = myDialog.getWindow();
WindowManager.LayoutParams param = window.getAttributes();
param.width = ;
param.height = ;
window.setAttributes(param);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
代码分析:
在onstartinputview()中创建新的线程;
执行showrateDialog()函数:
public class MyRateThread implements Runnable {
private static final String Time = "Time";
public MyRateThread() {
}
public void run() {
mRateSettings = getSharedPreferences(SETTING_INFOS, 0);
editor = mRateSettings.edit();
if ( mRateSettings.getBoolean(Rate_Finish, false) || mRateSettings.getBoolean(Dont_Remind, false)){
return ;
}
while(null == mInputView || !mInputView.isShown());
if (mRateSettings.getBoolean(Remind_Later, false)){
editor.putInt(Time, 0);
editor.putBoolean(Remind_Later, false);
editor.commit();
}
times = mRateSettings.getInt(Time, -1);
times++;
editor.putInt(Time, times);
editor.commit();
if( 0 != mRateSettings.getInt(Time, 0) && 0 == mRateSettings.getInt(Time, 0) % 15){
mHandler.post(new Runnable()
{
public void run() {
ShowRateDialog();
}
});
}
}
};
public void ShowRateDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
CharSequence itemSettings = this.getResources().getText(R.string.rate_now);
CharSequence itemSettings2 = this.getResources().getText(R.string.rate_later);
CharSequence itemSettings3 = this.getResources().getText(R.string.rate_never);
builder.setItems(new CharSequence[] { itemSettings, itemSettings2,
itemSettings3
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
dialog.dismiss();
switch(pos){
case 0:
//String PackName = "com.linpusime.android.linpuskbd";
String PackName = getPackageName();
Uri uri = Uri.parse("https://market.android.com/details?id="+PackName);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//AnySoftKeyboard.this.getApplicationContext().startActivity(intent);
//AnySoftKeyboard.this.startActivity(intent);
startActivity(intent);
editor.putBoolean(Rate_Finish, true);
editor.commit();
break;
case 1:
editor.putBoolean(Remind_Later, true);
editor.commit();
break;
case 2:
editor.putBoolean(Dont_Remind, true);
editor.commit();
break;
default:
;
}
}
});
builder.setTitle(this.getResources().getText(R.string.title));
myDialog = builder.create();
Window window = myDialog.getWindow();
WindowManager.LayoutParams param = window.getAttributes();
param.token = mInputView.getWindowToken();
param.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(param);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//myDialog.setCanceledOnTouchOutside(false);
myDialog.show();
}
fmoonstar 更新至2012.08.16