Android 利用AlertDialog 实现评分机制

本文介绍了如何在Android中利用AlertDialog构建评分机制。通过代码分析,展示了如何在非Activity环境中调用并显示评分对话框,包括获取和编辑SharedPreferences,以及在用户选择后更新设置的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要知识点:


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 between getDefaultSharedPreference() && 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值