start installed app from code
Hallo, I writing an app and I want to integrate a sketch-app to draw something and pass the file or path bac开发者_如何学编程k to my app. At the moment the app can store videos,pictures, sensor data,... and I like to add the ability to store sketches. I found this Code
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);
But i dont know how to start sketchbook(app from market). And I need also the path to the saved picture.Or is there an other way? Or Open Source ?
greetings...
So sketchbook is not your application?
To start another program, you need to know the intent filters it uses, or the package name. The developer of sketchbook will be able to supply you with this information (if they wish to share it).
Now when it comes to getting data FROM another app, the only way for that app to return anything to you is if the developer made it that way. It is up to the developer to decide what an Activity returns (if anything). So if you are looking to integrate with a specific application, it would be best for you to contact that developer and discuss how the two apps will interface with each other. On the other hand, if you implemented your own sketch app, you could make it integrate however you'd like.
You'd need to detect that the intent can be handled by the system.
public static boolean canHandleIntent(final Context context, final Intent intent) {
if (intent != null) {
final PackageManager pm = context.getPackageManager();
if (pm != null) {
if (pm.queryIntentActivities(intent, 0).size() > 0) {
return true;
}
}
}
return false;
}
If that returns false you should prompt the user to download the app from the market. Call,
//Market.getViewPackageOnMarket("org.otherapp.sketchapp");
public class Market {
public static final String Application_BaseMarketUri = "market://details?id=";
public static final Intent getViewPackageOnMarket(final String package_name) {
final Intent result = new Intent(Intent.ACTION_VIEW);
result.setData(Uri.parse(Application_BaseMarketUri + package_name));
return result;
}
}
You'll also need to use startActivityForResult, in your launching activity you need to add these things....
public class MyKillerAppActivity extends Activity {
// Declare constant for the activity result.
public static final int ACTIVITYRESULT_GETSKETCH = 1;
// Call the activity ....
public void someMethod() {
startActivityForResult(intent, ACTIVITYRESULT_GETSKETCH);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == ACTIVITYRESULT_GETSKETCH) {
handleGetSketchResult(data);
} /* Handle other results if you need to. */
}
}
}
That's kinda a mouthful but there's more! You'll need to interpret the data that comes back from the app. I don't know the format is for the sketch app you're looking into but here's an example on how to get data from the Gallery. The Gallery app returns you a content provider location to load the image that the user picked. This code locates the file path from the content provider and then opens the file and loads it into a Bitmap object for use.
public static Bitmap onActivityResult_getImage(Context context, Intent data) {
Bitmap result = null;
Uri dataUri = data.getData();
final String filePath = findPictureFilePath(context, dataUri);
if(filePath != null) {
result = BitmapFactory.decodeFile(filePath);
}
return result;
}
private static String findPictureFilePath(Context context, Uri dataUri) {
String filePath = null;
final String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(dataUri, projection, null, null, null);
int data_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if(cursor.moveToFirst()) {
filePath = cursor.getString(data_index);
}
} finally {
if(cursor != null) {
cursor.close();
}
}
return filePath;
}
精彩评论