jeudi 6 mars 2014

Post to social media using voice recognition?


Vote count:

0




I am building an application that uses voice recognition to perform all of its tasks. I want to extend the application to being able to post to social media with a voice command. I am not sure how to do this. I have only done sharing to social media using chooserIntent. In my attempt I use an intent and Action.SEND for sharing custom messages. I use a helper method to help me set things up:



private Intent getShareIntent(String type, String subject, String text)
{
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");

// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(share, 0);
System.out.println("resinfo: " + resInfo);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
share.putExtra(Intent.EXTRA_SUBJECT, subject);
share.putExtra(Intent.EXTRA_TEXT, text);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return null;
return share;
}
return null;
}


Then I want to do the sharing action in another method



private Intent shareIntent() {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/plain");

String shareMsg = "Share message example";

String emailMsg = "Email message email";

PackageManager pm = getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Name of Application");

if (TextUtils.equals(packageName, "com.google.android.gm")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email1@gmail.com", "email2@gmail.com"});
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailMsg);
} else if (TextUtils.equals(packageName, "com.android.email")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email1@gmail.com", "email2@gmail.com"});
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailMsg);
}

targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}

Intent facebookIntent = getShareIntent("facebook", "Name of App", shareMsg);
if(facebookIntent != null)
targetedShareIntents.add(facebookIntent);

Intent twitterIntent = getShareIntent("twitter", "Name of App", shareMsg);
if(twitterIntent != null)
targetedShareIntents.add(twitterIntent);

//here is where I'd like to somehow post to FB or Twitter (or even email me for feedback) automatically.

}


If I initialized a chooserIntent, this code would work fine. Something like this,



Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
"Share via");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
return shareIntent;


But how do I bypass the chooser and make my own choice to post based on voice command?



asked 35 secs ago






Aucun commentaire:

Enregistrer un commentaire