Android 4.2.2 API 17 Content Provider 报错 SecurityException: Permission Denial: opening provider

两个APK 通过Content Provider 来共享数据,在Android 4.2.2 API 17  之前都是正常的,但是到Android 4.2.2 API 17  上面就会报告异常类似如下的异常信息

09-17 12:15:52.221: E/AndroidRuntime(4551): FATAL EXCEPTION: main
09-17 12:15:52.221: E/AndroidRuntime(4551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.identifier.gamecenter.gctictactoe/com.identifier.gamecenter.game.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider from ProcessRecord{42622078 4551:com.identifier.gamecenter.gctictactoe/u0a10108} (pid=4551, uid=10108) that is not exported from uid 10072
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.os.Looper.loop(Looper.java:137)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at java.lang.reflect.Method.invokeNative(Native Method)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at java.lang.reflect.Method.invoke(Method.java:525)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at dalvik.system.NativeStart.main(Native Method)
09-17 12:15:52.221: E/AndroidRuntime(4551): Caused by: java.lang.SecurityException: Permission Denial: opening provider c.identifier.gamecenterapp.contentprovider.MyGamesContentProvider from ProcessRecord{42622078 4551:com.identifier.gamecenter.gctictactoe/u0a10108} (pid=4551, uid=10108) that is not exported from uid 10072
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.os.Parcel.readException(Parcel.java:1431)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.os.Parcel.readException(Parcel.java:1385)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2611)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4515)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2036)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1149)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.content.ContentResolver.query(ContentResolver.java:398)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.content.ContentResolver.query(ContentResolver.java:357)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at ch.ethz.csg.wlanopp.gapi.GameCenterController.getIdByGameTitle(GameCenterController.java:602)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at ch.ethz.csg.wlanopp.gapi.GameCenterController.isRegistered(GameCenterController.java:343)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at ch.ethz.csg.wlanopp.gapi.GameCenterController.addGame(GameCenterController.java:352)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at ch.ethz.csg.gamecenter.gctictactoe.MainActivity.onCreate(MainActivity.java:130)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.Activity.performCreate(Activity.java:5133)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-17 12:15:52.221: E/AndroidRuntime(4551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-17 12:15:52.221: E/AndroidRuntime(4551):     ... 11 more
09-17 12:20:52.487: I/Process(4551): Sending signal. PID: 4551 SIG: 9

在AndroidManifest.xml中原来的定义如下

<provider     
    android:name="com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider"
    android:authorities="com.identifier.gamecenterapp.contentprovider" >
</provider>

并且没有定义 android:targetSdkVersion

查找了很多地方,最终发现原因

Content providers are no longer exported by default. That is, the default value for the android:exported attribute is now “false". If it’s important that other apps be able to access your content provider, you must now explicitly set android:exported="true".

This change takes effect only if you set either android:targetSdkVersion or android:minSdkVersion to 17 or higher. Otherwise, the default value is still “true" even when running on Android 4.2 and higher.

具体的网页在 http://developer.android.com/about/versions/android-4.2.html

因此修改为如下即可 也就是增加 android:exported="true"

<provider     
android:name="com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider"
android:authorities="com.identifier.gamecenterapp.contentprovider"       
android:exported="true">
</provider>

Ubuntu 开机自动开启数字小键盘

每一次登录Ubuntu的时候小键盘都是关着的。可是每次又得输入密码,所以还是得手动打开数字键,亮了Numlock的灯。

Ubuntu 13.04 下面执行如下操作

sudo apt-get install numlockx

编辑文件

sudo vim /etc/lightdm/lightdm.conf

在文件最后增加

greeter-setup-script=/usr/bin/numlockx on

Ubuntu 13.10 下面执行如下操作

sudo apt-get install numlockx

编辑文件

sudo vim /etc/lightdm/lightdm.conf.d/50-unity-greeter.conf

在文件最后增加

greeter-setup-script=/usr/bin/numlockx on

Ubuntu 16.04 LTS 下面执行如下操作
创建文件

sudo vim /etc/lightdm/lightdm.conf.d/50-unity-greeter.conf

在文件最后增加

greeter-setup-script=/usr/bin/numlockx on

Ubuntu 13.10 运行VirtualBox虚拟机编译出现错误提示运行"/etc/init.d/vboxdrv setup"的问题

运行虚拟机提示:

The virtual machine 'XXX' has terminated unexpectedly during startup with exit code 1.

之后提示:

    Kernel driver not installed (rc=-1908)

    The VirtualBox Linux kernel driver (vboxdrv) is either not loaded or there is a permission problem with /dev/vboxdrv. Please reinstall the kernel module by executing

    '/etc/init.d/vboxdrv setup'

    as root. If it is available in your distribution, you should install the DKMS package first. This package keeps track of Linux kernel changes and recompiles the vboxdrv kernel module if necessary.

在终端下运行:

sudo /etc/init.d/vboxdrv setup

提示信息

Stopping VirtualBox kernel modules [ OK ]
* Recompiling VirtualBox kernel modules
* Look at /var/log/vbox-install.log to find out what went wrong

提示有错误,查看了一下错误日志

cat /var/log/vbox-install.log

内容如下

Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again。 停止。

根据提示应该是没有找到对应的源文件,查看了一下系统版本号:

uname -r

在进入/usr/src里发现果然没有对应的版本号头文件。

解决方法:

sudo apt-get install dkms build-essential linux-headers-generic
sudo /etc/init.d/vboxdrv setup

解决Eclipse中SVN 信息不显示的问题

Eclipse  中使用 svn 插件,原本正常,未作任何更改,最近几天突然eclipse 中查看文件时,文件后面的 版本号 、 文件的状态图标 等等都不见了。以为有插件冲突,卸载了好多其他的相关的插件,不行,卸载了SVN插件重新安装也不行。

检查插件都在,更新,提交等操作都能正常使用,唯独看不到状态了。

经过一番查找,找到问题解决办法。

Windows ,Linux 中

打开 : windows ->preferences->General->Appearance->Lable Decorations  勾选其中的 SVN 项即可

Mac

打开 : preferences->General->Appearance->Lable Decorations  勾选其中的 SVN 项即可

mvn报错:No goals have been specified for this build

选择【Run As】-【Maven build】的时候,报:

[INFO] Scanning for projects…[INFO]
[INFO] BUILD FAILURE[INFO]
[INFO] Total time: 0.109s[INFO] Finished at: Wed Apr 13 11:04:20 CST 2011[INFO] Final Memory: 1M/4M[INFO]
ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources,
process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-
test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy,
pre-clean, clean, post-clean. -> [Help 1][ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

eclipse安装的maven插件是m2eclipse,在控制台使用命令mvn compile并未报错。后在pom.xml文件<build>标签后面加上

<defaultGoal>compile</defaultGoal>

即可。

其实使用命令行时就已经指定了phase,而使用m2eclipse的【Run As】-【Maven build】时并未为其指定goal或phase,所以才报这个错误。

Struts2使用execAndWait在Action中调用getText报告java.lang.NullPointerException

使用 Struts2 编写页面,遇到一个要长时间运行的接口,因此增加了一个execAndWait ,结果在 Action 中调用 getText的时候报告异常

java.lang.NullPointerException
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:361)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:208)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:123)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103)
at com.infy.action.LoginAction.execute(LoginAction.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at org.apache.struts2.interceptor.BackgroundProcess$1.run(BackgroundProcess.java:57)
at java.lang.Thread.run(Thread.java:662)

查询了很多评论,最终找到原因跟解决方案,具体解释在 http://stackoverflow.com/questions/16692658/execandwait-interceptor-not-redirecting-to-success-page-after-waiting

  1. execAndWait causes the action to be executed in a new thread.
  2. Since ActionContext is ThreadLocal, the new thread will not get the values stored in the parent thread version of ActionContext. Every thread has a unique version of ActionContext
  3. getText() will throw a NPE when it tries to execute in the new thread because it depends onActionContext

简单解释一下,就是说

execAndWait 会导致执行的Action 在另外一个线程中被执行,而getText 依赖 ActionContext ,他从 ActionContext 中获得当前的Locale 从而根据语言的不同加载不同的文字,可是,由于ActionContext 是ThreadLocal 的,而execAndWait 新开线程的时候并没有把父线程的ActionContext 传递给子线程 结果导致在新开的子线程中的ActionContext中的数据都是null ,因此出现异常信息就不足为怪了。

解决方法为

To fix this, you need to copy the parent threads ActionContext into the execAndWait thread. You can do this by extending the BackgroundProcess class, implementing the beforeInvocation()and afterInvocation() methods, and extending ExecuteAndWaitInterceptor, implementing the getNewBackgroundProcess() method.

代码例子如下,注意,原文中作者的代码存在多线程同步问题,具体体现在

beforeInvocation

被调用的时候,得到的 context 为null ,导致注入失败。

因此需要重载两个类,来解决这个问题

import org.apache.struts2.interceptor.BackgroundProcess;
import org.apache.struts2.interceptor.ExecuteAndWaitInterceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

public class ExecAndWaitInterceptorEx extends ExecuteAndWaitInterceptor {

	private static final long serialVersionUID = -4456744368791451159L;
	  /**
     * {@inheritDoc}
     */
    @Override
    protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
    	ActionInvocationEx aActionInvocationEx = new ActionInvocationEx(arg1,ActionContext.getContext());
        return new BackgroundProcessEx(arg0, aActionInvocationEx, arg2);
    }

    private class BackgroundProcessEx extends BackgroundProcess {
		public BackgroundProcessEx(String threadName,
				ActionInvocation invocation, int threadPriority) {
			super(threadName, invocation, threadPriority);
		}

		private static final long serialVersionUID = -9069896828432838638L;
        /**
         * {@inheritDoc}
         * @throws InterruptedException 
         */
        @Override
        protected void beforeInvocation() throws InterruptedException {
        	ActionInvocationEx aActionInvocationEx = (ActionInvocationEx)this.invocation;
        	ActionContext context = aActionInvocationEx.getContext();
            ActionContext.setContext(context);
        }

        /**
         * {@inheritDoc}
         */
       @Override
        protected void afterInvocation() {
            ActionContext.setContext(null);
        }

    }
}
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionEventListener;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.ValueStack;

public class ActionInvocationEx implements ActionInvocation {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final ActionInvocation mActionInvocation;

	private final ActionContext context;

	public ActionInvocationEx(ActionInvocation aActionInvocation,ActionContext aContext)
	{
		mActionInvocation = aActionInvocation;
		context = aContext;
	}

	public Object getAction() {
		return mActionInvocation.getAction();
	}

	public boolean isExecuted() {
		return mActionInvocation.isExecuted();
	}

	public ActionContext getInvocationContext() {
		return mActionInvocation.getInvocationContext();
	}

	public ActionProxy getProxy() {
		return mActionInvocation.getProxy();
	}

	public Result getResult() throws Exception {
		return mActionInvocation.getResult();
	}

	public String getResultCode() {
		return mActionInvocation.getResultCode();
	}

	public void setResultCode(String resultCode) {
		mActionInvocation.setResultCode(resultCode);
	}

	public ValueStack getStack() {
		return mActionInvocation.getStack();
	}

	public void addPreResultListener(PreResultListener listener) {
		mActionInvocation.addPreResultListener(listener);
	}

	public String invoke() throws Exception {
		return mActionInvocation.invoke();
	}

	public String invokeActionOnly() throws Exception {
		return mActionInvocation.invokeActionOnly();
	}

	public void setActionEventListener(ActionEventListener listener) {
		mActionInvocation.setActionEventListener(listener);
	}

	public void init(ActionProxy proxy) {
		mActionInvocation.init(proxy);
	}

	public ActionInvocation serialize() {
		return mActionInvocation.serialize();
	}

	public ActionInvocation deserialize(ActionContext actionContext) {
		return mActionInvocation.deserialize(actionContext);
	}

	/**
	 * @return the context
	 */
	public ActionContext getContext() {
		return context;
	}
}

写完之后,在struts.xml 中配置一下拦截器,覆盖掉默认的拦截器,下面是我的配置例子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.i18n.encoding" value="utf-8"></constant>
	<constant name="struts.multipart.maxSize" value="20971520"/>
    <constant name="struts.devMode" value="true" />

	<package name="FaeSupport" namespace="/" extends="struts-default">
	    <!-- 安装自定义的 execAndWait 拦截器,覆盖掉系统默认的,目的是解决在 execAndWait 出现的时候,Action调用 getText 报java.lang.NullPointerException-->
	    <interceptors > 
            <interceptor name="execAndWait" class="com.FaeSupport.Interceptor.ExecAndWaitInterceptorEx"/> 
        </interceptors > 
		<action name="faesupport" class="com.FaeSupport.Action.FaeSelAction" method="SelParametersCheck">
        <interceptor-ref name="defaultStack"/>
        <interceptor-ref name="execAndWait">
            <param name="delay">1500</param>
        </interceptor-ref>
        <result name="wait">/build_wait.jsp</result>			
		<result name="success">/build_wait.jsp</result>
		</action>
	</package>

</struts>

为Struts 2应用程序创建进度条(等待页面)

Struts 2模拟进度条的原理

对于一些需要较长时间才能完成的任务,在Web开发中,会由HTTP协议会因为超时而断开而面临许多风险,这是在桌面开发不曾遇到的。Struts 2提供的execAndWait拦截器就是为了处理和应付这种情况而设计的。注意,该拦截器不在"defaultStack"中,所以必须在使用它的动作里声明它,并且必须放在拦截器栈的最后一个。

使用了该拦截器后,动作依然正常执行,只是该拦截器会分配一个后台线程处理动作的运行,并在动作完成之前把用户带到一个"等待"页面。,该页面每隔一段时间刷新一次,直到那个后台线程执行完毕为止。如果用户随后又触发了同一个动作,但顶一个动作尚未执行完毕,这个拦截器将继续向用户发送"等待"结果;如果他已经执行完毕,用户会看到该动作的最终结果。
"等待"结果的行为与"dispatcher"结果的行为很相似,但是要注意的是,"等待"结果对应的视图带有如下的meta标签:

<meta http-equiv="refresh" content="5;url=/Struts2/default_progressbar.action"/>

该标签的作用就每隔多少秒就重新加载一次同样的URL。"5"是5秒,"url=/Struts2/default_progressbar.action"表示要加载的URL。
Struts 2是一个灵活强大的框架,如果你不喜欢Struts 2提供的默认"等待页面",你也可以自己设计自己的等待页面,若在动作声明中,没有找到"等待"结果,将使用默认值。

execAndWait拦截器

execAndWait拦截器 可以接收以下参数:

  • threadPriority:分配给相关线程的优先级,默认值为Thread.NORM_PRIORITY。
  • delay:向用户发送"等待"结果前的毫秒数,默认值为0。如果你不想立刻发送"等待"结果,可以将该参数设置为一个值。例如,你想让动作超过2秒还未完成时才发送"等待"结果,需要将其值设置为2000.
  • delaySleepInterval:每隔多少毫秒唤醒主线程(处理动作的后台线程)去检查后台线程是否已经处理完成,默认值是100。这个值设为0时无效。

示例:使用默认视图与自定义视图

创建一个动作类,该动作类的工作为挂起30秒:

public class ProgressbarAction extends ActionSupport
{
    private static final long serialVersionUID = 7441785390598480063L;
    private int  complete = 0;

    // 获取进度值
    public int getComplete()
    {
        complete += 10;
        return complete;
    }

    @Override
    public String execute()
    {
        try
        {
            Thread.sleep(30000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return SUCCESS;
    }
}

配置struts.xml文件:

<package name="progressbar" extends="struts-default">
    <action name="default_progressbar" class="struts2.suxiaolei.progressbar.action.ProgressbarAction">
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="execAndWait">
            <param name="delay">1500</param>
        </interceptor-ref>
        <result name="success">/state_ok.jsp</result>
    </action>

    <action name="customer_progressbar" class="struts2.suxiaolei.progressbar.action.ProgressbarAction">
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="execAndWait">
            <param name="delay">1500</param>
        </interceptor-ref>
        <result name="wait">/customer_wait.jsp</result>
        <result name="success">/state_ok.jsp</result>
    </action>
</package>

测试页面:

<body>
    <s:a href="/Struts2/default_progressbar.action">default_view</s:a>
    <br />
    <s:a href="/Struts2/customer_progressbar.action">customer_view</s:a>
</body>

自定义等待页面:

<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'customer_wait.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <!-- 下面的meta元素才是重点,其他的没什么影响,是IDE自带的  -->
    <meta http-equiv="refresh" content="3;url=/Struts2/customer_progressbar.action">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <div>
        Please wait...(<s:property value="complete"/>)% complete
    </div>
  </body>
</html>

最终结果页面:

<body>
    OK!
</body>

在浏览器中输入:http://localhost:8081/Struts2/test.jsp,获得如下页面2011110117040626

首先点击,"default_view"链接:
2011110117061560
查看它的源代码:

<html>
    <head>
        <meta http-equiv="refresh" content="5;url=/Struts2/default_progressbar.action"/>
    </head>
    <body>
        Please wait while we process your request...
        <p/>

        This page will reload automatically and display your request when it is completed.
    </body>
</html>

这次点击"customer_view"链接:

2011110117104414

2011110117105659

这是自定义界面,最后动作执行完毕后,都会获得最终页面

2011110117121914

引用自:http://www.blogjava.net/athrunwang/archive/2011/11/18/364200.html

Mac图片圈点软件Skitch

一直想在Mac下面找一个免费的好用的图片圈点,截图,截屏软件,增加点注释啊,画个箭头啊什么简单的功能,只要好用即可,找了半天,总算找到一个好用的软件Skitch。 在Mac商店中搜索Skitch ,然后下载即可。现在是免费的了,被Evernote收购之后就免费了,蛮好用的,强烈推荐。