Android Process

I would agree that not many people would find android:process to be useful as an attribute of the application tag. However, I have found it to be useful as an attribute of the activity tag.

The purpose of android:process on an activity is to specify that your activity should be launched in a process having a specific name. The choice of that name may be used either to isolate the activity in its own process (different from the one that launched it), or to force it to cohabit in a single process with other activities that use the same name.

Per the Dev Guide (http://developer.android.com/guide/topics/manifest/activity-element.html):

“If the name assigned to this attribute begins with a colon (‘:’), a new process, private to the application, is created when it’s needed and the activity runs in that process. If the process name begins with a lowercase character, the activity will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.”

I have recently found this attribute to be useful in solving a problem I had with launching a help activity for an app that, under certain circumstances, was fairly close to the 16MB heap limit that still applies to some devices. Launching its help activity was, in those situations, pushing my app over the limit, resulting in a force close.

By using the android:process tag, I was able to specify that my help activity should be launched in a separate process of its own. This process had its own 16MB heap, and it was not counted against the heap of my main app that launched it. This permanently and completely prevented my app from running out of heap space and crashing when help was launched.

If your launching app has the package name

com.mycompany.mymainapp and is therefore assigned a process name that is that same string, then, if you use

android:process=”:myhelp” on your launched activity, it will be assigned the process name

com.mycompany.mymainapp:myhelp and that process will have its own, separate process ID, which you can view (for example in DDMS).

That, at least, has been my experience. My testing has so far been performed on an old Moto Droid running CM6 (Android 2.2.1), configured to have a 16MB heap limit.

In my case, since I did not want the user to perceive the help as being separate from my app, I included the

android:excludeFromRecents=”true” attribute to prevent the help activity from appearing on the recent apps (long-press Home) list. I also included

android:taskAffinity=”com.mycompany.mymainapp.HelpActivity” where HelpActivity is the name of the help activity, to segregate the activity in its own task

I also added:

android:launchMode=”singleInstance” to prevent multiple instances of this app from being created each time the user invoked help.

I also added the flag:

Intent.FLAG_ACTIVITY_NEW_TASK to the Intent used to launch the help activity.

These parameters may or may not be needed, depending upon the use that you are making of the android:process attribute.

Considering how often one encounters memory limits when developing for Android devices, having a technique that can, in some cases, allow you to break out parts of your app into separate processes, each with its own heap, seems like a wonderful gift. There may be hidden hazards in doing this that I have not yet considered or experienced, but so far, so good, in my particular instance.


Android launchmode

activity 启动顺序 A-B-C 如果 再启动B

根据设置参数的不同 结果也不同

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

结果 A-B-C-B

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_CLEAR_TOP);

结果 A-B

B设置android:launchMode=”singleTask”

结果 A-B


test

测试

private void releasePage() {
		ParserHelper.getParserHelper().clearInfo(MODE_DATA.DATA_DETAIL);
		ParserHelper.getParserHelper().cancle(MODE_DATA.DATA_DETAIL);
		if (mResembleMovieView != null) {
			mResembleMovieView.release();
		}
		postershow.release();
	}

mm

测试

private void releasePage() {
		ParserHelper.getParserHelper().clearInfo(MODE_DATA.DATA_DETAIL);
		ParserHelper.getParserHelper().cancle(MODE_DATA.DATA_DETAIL);
		if (mResembleMovieView != null) {
			mResembleMovieView.release();
		}
		postershow.release();
	}

Android WebView上传文件

在这次食物上传功能的开发中,其中WebView中上传图片需要调用系统的文件系统,默认WebView是不支持文件上传的,需要自己手动配置一些东西,具体代码如下:

public class BrowserActivity extends Activity {
    private WebView mWebView;
    private ValueCallback<Uri> mUploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;

    public void onCreate(Bundle outState) {
        super.onCreate(outState);
        setContentView(R.layout.activity_browser);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(new MyWebClient());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
            return;
        Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
        }
    }

    public class MyWebClient extends WebChromeClient {
        // For Android 3.0-
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    }
}

主要就是自定义了WebChromeClient,然而测试时发现在4.4以上的Android版本依然不可以,搜了下openFileChooser方法在4.4以后不是public的,所以以后不建议这种直接在WebView上传文件的做法。

stackoverflow参考链接:HTML file input in android webview (android 4.4, kitkat)