Android 调用已安装市场,去应用市场评分

最近需求要实现这样一个功能,在某个界面停留40多秒后,弹出一个Dialog,让用户去市场评分,或反馈意见。

 
   
/**
* Created by Administrator on 2016/2/22.
*

* 用于提示用户评分工具类
*/
 
public class GradeUtil {
 
private Timer timer;
private Context mContext;
 
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (null != mContext) {
LayoutInflater mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.m_user_rating_dialog, null, false);
Dialog dialog = DialogUtil.showUserRatingDialog(mContext, view, null); //弹出Dialog
MyClickListener myClickListener = new MyClickListener(dialog);
view.findViewById(R.id.iv_good_reputation).setOnClickListener(myClickListener);
view.findViewById(R.id.iv_suggestion).setOnClickListener(myClickListener);
view.findViewById(R.id.tv_reject).setOnClickListener(myClickListener);
SharedPreferencesUtils.setParam(mContext, Constants.VERSION_KEY, BuildConfig.VERSION_CODE); //如果弹出了Dialog,则修改SP版本为当前版本
stop();
}
}
};
 
public void start(final Context context) {
mContext = context;
if (isShowRatingDialog()) {
LogUtil.d("GradeUtil", "mGradeUtil.start()");
timer = new Timer();
timer.schedule(new MyTimerTask(handler), 45000); //延迟45秒执行
}
}
 
/**
* 计时器
*/
public static class MyTimerTask extends TimerTask {
 
Handler handler;
 
MyTimerTask(Handler handler){
this.handler = handler;
}
 
@Override
public void run() {
handler.obtainMessage().sendToTarget();
}
}
 
public void stop() {
LogUtil.d("GradeUtil", "mGradeUtil.stop()");
if (null != timer) {
timer.purge();
timer.cancel();
timer = null;
}
}
 
static class MyClickListener implements View.OnClickListener {
 
Dialog mDialog;
 
MyClickListener(Dialog dialog){
mDialog = dialog;
}
 
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_good_reputation:
try {
Uri uri = Uri.parse("market://details?id=" + v.getContext().getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
mDialog.cancel();
} catch (Exception e) {
e.printStackTrace();
ToastUtil.showShort(v.getContext(), "没找到应用市场!");
mDialog.cancel();
}
break;
case R.id.iv_suggestion:
FeedbackAgent agent = new FeedbackAgent(v.getContext());
agent.startFeedbackActivity();
mDialog.cancel();
break;
case R.id.tv_reject:
mDialog.cancel();
break;
}
}
}
 
private boolean isShowRatingDialog() {
int currentVersion = BuildConfig.VERSION_CODE;
int lastVersion = (int) SharedPreferencesUtils.getParam(mContext, Constants.VERSION_KEY, 0); //用SP 记录版本号
if (currentVersion > lastVersion ) {
//如果当前版本大于上次版本,该版本属于第一次启动
return true;
} else {
return false;
}
}
 
}

实现方法如下, 在onCreate中 mGradeUtil = new GradeUtil(); new出该工具类实例

在onResume()中调用 mGradeUtil.start(NewHomeTeamMainActivity.this);     开始方法
在onStop() 中调用 mGradeUtil.stop();

你可能感兴趣的:(Android,java)