"android.permission.CAMERA" giving error on including in AndroidManifes.xml file - android

this is my AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mycamera">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and this is the corresponding java file for the above code:
package com.example.mycamera;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button btn;
ImageView imagedisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.image);
imagedisplay = (ImageView)findViewById(R.id.image_capture);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent , 0);
}
});
}
#Override
public void onActivityResult(int requestCode , int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
Bitmap bitmap= (Bitmap) data.getExtras().get("data");
imagedisplay.setImageBitmap(bitmap);
}
}
this is working fine without even including:
<uses-permission android:name="android.permission.CAMERA"/>
in AndroidManifest.xml file.
But on including the camera permission it is not working and showing the error your app has stopped working.

private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private static final int MY_CAMERA_PERMISSION_CODE = 100;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

Try once this :
private boolean isVersionBelowMarshmallow = false;
public boolean checkPermissionForCamera() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
isVersionBelowMarshmallow = true;
}
if (isVersionBelowMarshmallow)
return true;
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
return result == PackageManager.PERMISSION_GRANTED;
}

Try these Solution Once :
if (checkAndRequestPermissions(getContext())) {
}
private boolean checkAndRequestPermissions(Context context) {
int ExtstorePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
List<String> listPermissionsNeeded = new ArrayList<>();
if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (ExtstorePermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "FlagUp Requires Access to Your Storage.", Toast.LENGTH_SHORT).show();
// finish();
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "FlagUp Requires Access to Camara.", Toast.LENGTH_SHORT).show();
// finish();
}
}

Related

Capturing screenshots on background

I'm using ShotWatch to capture screenshots events.
The included example only works while inside the aplication, but I want to capture the event and launch an Activity. My code seems to ignore the service.
The mainfest has all the services/activity defined.
MainActivity:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = findViewById(R.id.imageView);
// run-time permissions
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
Toast.makeText(this, ":(", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// You can use the API that requires the permission.
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
startService(new Intent(MainActivity.this, ScreenshotCaptureService.class));
}
}
}
ScreenshotCaptureService:
public class ScreenshotCaptureService extends Service implements ShotWatch.Listener {
private ShotWatch shotWatch;
#Override
public void onCreate() {
super.onCreate();
this.shotWatch = new ShotWatch(getContentResolver(), this);
this.shotWatch.register();
}
#Override
public void onDestroy() {
super.onDestroy();
this.shotWatch.unregister();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
#Override
public void onScreenShotTaken(ScreenshotData data) {
Intent intent = new Intent(this, AddActivity.class);
intent.putExtra(AddActivity.CAPTURE_PATH, data.getPath());
startService(intent);
}
}
AddActivity:
public class AddActivity extends AppCompatActivity {
public static final String CAPTURE_PATH = "ADD_CAPTURE_PATH";
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Uri captura = Uri.fromFile(new File(getIntent().getStringExtra(AddActivity.CAPTURE_PATH)));
this.imageButton = findViewById(R.id.imageButton);
this.imageButton.setImageURI(captura);
this.imageButton.setOnClickListener((View v)->{
// ...
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
switch (requestCode) {
// ...
}
}
}
Fixed.
I was using 'startService' instead of 'startActivity' inside onScreenShotTaken, on ScreenshotCaptureService.

phone call is not happenning in my android app?

While making a call from the app, it is not working. I have added the permission in android manifest, but it is still not working. Please see my code and help me soon.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:999999999"));
if (ActivityCompat.checkSelfPermission(menu.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
This code should work:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
Permission in Manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
but i highly recommend using ACTION_DIAL instead. It opens up the dialer screen with the number entered instead. This gives the user more flexibility. Also you don't need to have the CALL_PHONE permission with this one.
Here is an update:
With CALL_PHONE permission
Main Class
public class MainActivity extends AppCompatActivity {
private final int CALL_PHONE = 1;
private Button dialBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialBtn = (Button) findViewById(R.id.dial_button);
//In android 6 we need to ask for permissions:
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
} else {
Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
}
} else {
setupView();
}
}
private void setupView() {
dialBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:999999999"));
// We have to implement this part because ... yeah permissions in android.....
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
return;
}
startActivity(callIntent);
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case CALL_PHONE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setupView();
} else {
Toast.makeText(getApplicationContext(), "We need permissions to dial.", Toast.LENGTH_LONG).show();
}
break;
}
}
My manifest:
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Without the permission check and only using ACTION_DIAL
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dialBtn = (Button) findViewById(R.id.dial_button);
dialBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:999999999"));
startActivity(callIntent);
}
});
}
}
The last one is so much easier. Android permission checks are a pain in the ass.
I just made my own class. It's easier for the next time to also provide the error Stack trace. But for what I can see in your code is that your using menu.this I don't know for sure because u didn't provide enough information but I think this causes the error.
I hope I helped u out.

ACTION_USER_PRESENT not working when app not in background

I am using ACTION_USER_PRESENT Broadcast Receiver in my application,
My Problem is that : I am getting the BroadCastReceiver only when my application is in Pause state.
Here is my manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver
android:name=".utils.receivers.ReminderBroadcastReceiver"
android:enabled="true"
android:exported="true" />
<receiver android:name=".utils.receivers.UserPresentBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
My Receiver:
public class UserPresentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/*Sent when the user is present after
* device wakes up (e.g when the keyguard is gone)
* */
// MY STUFF - which works when my app is on paused state, but not when it is closed
}
}
am running on Marshmallo 6.0
Any help?
Getting runtime permision ofr Marshmallo 6.0 or greator.
public class RuntimePermission extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 123;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (CheckPermission(RuntimePermission.this, Manifest.permission.READ_PHONE_STATE)) {
// you have permission go ahead
} else {
// you do not have permission go request runtime permissions
RequestPermission(RuntimePermission.this, Manifest.permission.READ_PHONE_STATE, REQUEST_RUNTIME_PERMISSION);
}
}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have permission go ahead
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}

Error when I try to start the service from main Activity

I have an Android app that in the main class start a service. So I have this error
06-09 09:34:00.880 26760-26760/it.eresult.decipher E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service it.eresult.decipher.service.CriticalInformationService#4111f448 with Intent { cmp=it.eresult.decipher/.service.CriticalInformationService }: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W#41117e00 -- permission denied for this window type
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)
at android.app.ActivityThread.access$1900(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W#41117e00 -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:537)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at it.eresult.decipher.service.CriticalInformationService.onStartCommand(CriticalInformationService.java:37)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
at android.app.ActivityThread.access$1900(ActivityThread.java:123) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
at dalvik.system.NativeStart.main(Native Method) 
This is my AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.eresult.decipher">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:background="#color/colorToolBar">
<activity
android:name=".activity.MainActivity">
</activity>
<activity
android:name=".activity.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.CriticalInformationService"
android:label="Critical Information Service"
>
</service>
<uses-permission
android:name="android.permission.SYSTEM_ALERT_WINDOW" />
</application>
</manifest>
With this code, I try to start the service:
startService(new Intent(this, CriticalInformationService.class));
Then, this is the Service:
public class CriticalInformationService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View mView = mInflater.inflate(R.layout.settings_activity, null);
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);
mWindowManager.addView(mView, mLayoutParams);
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
}
EDIT
This is the MainActivity that launch Service
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkPremission();
//startService(new Intent(this, CriticalInformationService.class));
}
void checkPremission() {
//select which permission you want
final String permission = Manifest.permission.SYSTEM_ALERT_WINDOW;
// if in fragment use getActivity()
if (ContextCompat.checkSelfPermission(LoginActivity.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, permission)) {
} else {
ActivityCompat.requestPermissions(LoginActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION);
}
} else {
// you have permission go ahead launch service
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead launch service
}else{
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
How can I fix this error?
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkPremission();
//startService(new Intent(this, CriticalInformationService.class));
}
void checkPremission() {
//select which permission you want
final String permission = Manifest.permission.SYSTEM_ALERT_WINDOW;
// if in fragment use getActivity()
if (ContextCompat.checkSelfPermission(LoginActivity.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, permission)) {
} else {
ActivityCompat.requestPermissions(LoginActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION);
}
} else {
// you have permission go ahead launch service
startService(new Intent(this, CriticalInformationService.class));
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead launch service
startService(new Intent(this, CriticalInformationService.class));
}else{
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

Voice to text: No Activity found to handle Intent

I am trying to use google search to text engine but it is not possible and get error :
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras)
Can you help me?
Here is my MainActivity.java code
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1234;
Button Start;
TextView Speech;
Dialog match_text_dialog;
ListView textlist;
ArrayList<String> matches_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Start = (Button)findViewById(R.id.start_reg)
Speech = (TextView)findViewById(R.id.speech);
Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isConnected()){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, REQUEST_CODE);
}
else{
Toast.makeText(getApplicationContext(), "Plese Connect to Internet", Toast.LENGTH_LONG).show();
}}
});
}
public boolean isConnected()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
if (net!=null && net.isAvailable() && net.isConnected()) {
return true;
} else {
return false;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
match_text_dialog = new Dialog(MainActivity.this);
match_text_dialog.setContentView(R.layout.dialog_matches_frag);
match_text_dialog.setTitle("Select Matching Text");
textlist = (ListView)match_text_dialog.findViewById(R.id.list);
matches_text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, matches_text);
textlist.setAdapter(adapter);
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Speech.setText("You have said " +matches_text.get(position));
match_text_dialog.hide();
}
});
match_text_dialog.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
and this is AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learn2crack.speech"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learn2crack.speech.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Categories

Resources