澳门新葡萄京官网首页 6

封装Activity相关工具类

在 Adapter 中 startActivity 时,现身谬误:

/**

澳门新葡萄京官网首页 1

android.util.AndroidRuntimeException: Calling startActivity() from
outside of an Activity context requires the FLAG_澳门新葡萄京官网首页 ,ACTIVITY_NEW_TASK
flag. Is this really what you want?

* @author vondear

public classApdteextendsRecyclerView.Adapter {

原来:

* @date 2016/1/24

private Context context;

盛传给 Adapter 的 context 不对,应该是某些 Activity 的 context,误传为了
getApplicationContext(卡塔尔(قطر‎。

*

private List<String> list;

* 封装Activity相关工具类

public Apdte(Context context,List<String> list ){

*/

this.context= context;

public class ActivityTool {

this.list= list;

private static StackactivityStack;

}

    /**

public Apdte() {

* 添加Activity 到栈

super();

*

}

    * @param activity

public MyApdate onCreate ViewHolder(ViewGroup parent, intviewType) {

    */

View view =
LayoutInflater.from.inflate(R.layout.recyclerview_header,parent,
false);

    public static void addActivity(Activity activity) {

MyApdate myApdate =newMyApdate;

if (activityStack ==null) {

return myApdate;

activityStack =new Stack<>();

}

        }

@Override

activityStack.add(activity);

public void onBindViewHolder(MyApdate holder, intposition) {

    }

holder.tt.setText(list.get);

/**

holder.tt.setOnClickListener(newView.OnClickListener() {

* 获取当前的Activity(仓库中最终一个压入的卡塔尔国

@Override

*/

public void onClick {

    public static ActivitycurrentActivity() {

Intent in =newIntent(v.getContext(),Second_Activity.class);

Activity activity =activityStack.lastElement();

Log.d(“ssss”,”单个的”);

        return activity;

v.getContext().startActivity;

    }

}

/**

});

* 甘休近日Activity(仓库中最后一个压入的)

}

*/

@Override

    public static void finishActivity() {

public intgetItemCount() {

Activity activity =activityStack.lastElement();

return list.size();

    }

}

/**

}

* 甘休钦定的Activity

class MyApdate extends RecyclerView.ViewHolder {

*

TextView tt;

    * @param activity

public MyApdate(finalView itemView) {

    */

super;

    public static void finishActivity(Activity activity) {

tt= itemView.findViewById(R.id.TextView);

if (activity !=null) {

itemView.setOnClickListener(newView.OnClickListener() {

activityStack.remove(activity);

@Override

            activity.finish();

public voidonClick {

            activity =null;

Intent in =new Intent(itemView.getContext(),Second_Activity.class);

        }

Log.d(“ssss”,”全部的”);

}

itemView.getContext().startActivity;

/**

}

* 甘休内定类名的Activity

});

*/

}

    public static void finishActivity(Class cls) {

}

for (Activity activity :activityStack) {

public classMainActivityextendsAppCompatActivity {

if (activity.getClass().equals(cls)) {

privateXRecyclerViewxx;

finishActivity(activity);

privateApdteaa;

            }

privateListlist;

}

@Override

}

protected voidonCreate(Bundle savedInstanceState) {

/**

super.onCreate(savedInstanceState);

* 结束全数的Activity

setContentView(R.layout.activity_main);

*/

xx= (XRecyclerView) findViewById(R.id.XRecyclerView);

    public static void finishAllActivity() {

LinearLayoutManager layoutManager =newLinearLayoutManager;

int size =activityStack.size();

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        for (int i =0; i < size; i++) {

xx.setLayoutManager(layoutManager);

if (null !=activityStack.get(i)) {

list=newArrayList<>();

activityStack.get(i).finish();

for(inti =0;i <20;i++) {

            }

list.add(“第”+ i +”——–“);

}

}

activityStack.clear();

aa=newApdte(MainActivity.this,list);

    }

xx.setAdapter;

public static void AppExit(Context context) {

xx.setLoadingListener(newXRecyclerView.LoadingListener() {

try {

@Override

finishAllActivity();

public voidonRefresh() {

            ActivityManager activityManager = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);

// list.clear();

            activityManager.restartPackage(context.getPackageName());

for(inti =0;i <20;i++) {

            System.exit(0);

list.add(“第”+ i +”条数据”);

        }catch (Exception e) {

}

}

aa=newApdte(MainActivity.this,list);

}

aa.notifyDataSetChanged();

public static StackgetActivityStack() {

xx.refreshComplete();

return activityStack;

}

    }

@Override

/**

public voidonLoadMore() {

* 剖断是还是不是留存钦赐Activity

list.add;

*

for(inti =0;i <10;i++) {

    * @param context    上下文

list.add(“第”+ i +”条数据”);

    * @param packageName 包名

}

    * @param className  activity全路线类名

aa.notifyDataSetChanged();

    * @return {@code true}: 是
{@code false}: 否

xx.loadMoreComplete();

*/

}

    public static boolean isExistActivity(Context context, String
packageName, String className) {

});

Intent intent =new Intent();

}

        intent.setClassName(packageName, className);

}

        return !(context.getPackageManager().resolveActivity(intent, 0)
==null ||

澳门新葡萄京官网首页 2澳门新葡萄京官网首页 3澳门新葡萄京官网首页 4澳门新葡萄京官网首页 5

intent.resolveActivity(context.getPackageManager()) ==null ||

apply plugin:’com.android.application’

context.getPackageManager().queryIntentActivities(intent, 0).size()
==0);

android {

    }

compileSdkVersion 25

/**

buildToolsVersion “25.0.2”

* 展开钦赐的Activity

defaultConfig {

*

applicationId “com.bawei.xrecycleView”

    * @param context    上下文

minSdkVersion 15

    * @param packageName 包名

targetSdkVersion 25

    * @param className  全类名

versionCode 1

*/

versionName “1.0”

    public static void launchActivity(Context context, String
packageName, String className) {

}

launchActivity(context, packageName, className, null);

buildTypes {

    }

release {

/**

minifyEnabled false

* 张开钦赐的Activity

proguardFiles getDefaultProguardFile(‘proguard-android.txt’),

*

‘proguard-rules.pro’

    * @param context    上下文

}

    * @param packageName 包名

}

    * @param className  全类名

}

    * @param bundle      bundle

dependencies {

*/

compile fileTree(dir:’libs’,include: [‘*.jar’])

    public static void launchActivity(Context context, String
packageName, String className, Bundle bundle) {

testCompile’junit:junit:4.12′

context.startActivity(RxIntentTool.getComponentNameIntent(packageName,
className, bundle));

compile’com.android.support:appcompat-v7:25.1.0′

    }

compile’com.jcodecraeer:xrecyclerview:1.3.2′

/**

}

* 供给最低API为11


* Activity 跳转

澳门新葡萄京官网首页 6

* 跳转后Finish以前全数的Activity

*

    * @param context

    * @param goal

    */

    public static void skipActivityAndFinishAll(Context context, Class
goal, Bundle bundle) {

Intent intent =new Intent(context, goal);

        intent.putExtras(bundle);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);

        context.startActivity(intent);

        ((Activity) context).finish();

    }

/**

* 须求最低API为11

* Activity 跳转

* 跳转后Finish在此之前全部的Activity

*

    * @param context

    * @param goal

    */

    public static void skipActivityAndFinishAll(Context context, Class
goal) {

Intent intent =new Intent(context, goal);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);

        context.startActivity(intent);

        ((Activity) context).finish();

    }

/**

* Activity 跳转

*

    * @param context

    * @param goal

    */

    public static void skipActivityAndFinish(Context context, Class
goal, Bundle bundle) {

Intent intent =new Intent(context, goal);

        intent.putExtras(bundle);

        context.startActivity(intent);

        ((Activity) context).finish();

    }

/**

* Activity 跳转

*

    * @param context

    * @param goal

    */

    public static void skipActivityAndFinish(Context context, Class
goal) {

Intent intent =new Intent(context, goal);

        context.startActivity(intent);

        ((Activity) context).finish();

    }

/**

* Activity 跳转

*

    * @param context

    * @param goal

    */

    public static void skipActivity(Context context, Class goal) {

Intent intent =new Intent(context, goal);

        context.startActivity(intent);

    }

/**

* Activity 跳转

*

    * @param context

    * @param goal

    */

    public static void skipActivity(Context context, Class goal, Bundle
bundle) {

Intent intent =new Intent(context, goal);

        intent.putExtras(bundle);

        context.startActivity(intent);

    }

public static void skipActivityForResult(Activity context, Class goal,
int requestCode) {

Intent intent =new Intent(context, goal);

        context.startActivityForResult(intent, requestCode);

    }

public static void skipActivityForResult(Activity context, Class goal,
Bundle bundle, int requestCode) {

Intent intent =new Intent(context, goal);

        intent.putExtras(bundle);

        context.startActivityForResult(intent, requestCode);

    }

/**

* 获取launcher activity

*

    * @param context    上下文

    * @param packageName 包名

    * @return launcher activity

*/

    public static StringgetLauncherActivity(Context context, String
packageName) {

Intent intent =new Intent(Intent.ACTION_MAIN, null);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        PackageManager pm = context.getPackageManager();

        List infos = pm.queryIntentActivities(intent, 0);

        for (ResolveInfo info : infos) {

if (info.activityInfo.packageName.equals(packageName)) {

return info.activityInfo.name;

            }

}

return “no ” + packageName;

    }

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注