Android Torch App not working in Marshmallow - android

If i give permission manually than it works. Rather than that it doesn't work. Can any one help me. What change should i made. I am new to android development. I can't find anything on google.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plutianclub.flashlight">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_flashlight_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Main Activity:
package com.plutianclub.flashlight;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.offButton);
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
camera = Camera.open();
parameters = camera.getParameters();
isFlash = true;
}
imageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (isFlash == true){
if (isOn == false ){
imageButton.setImageResource(R.drawable.on);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
isOn = true;
}else {
imageButton.setImageResource(R.drawable.off);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
isOn = false;
}
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Error....");
builder.setMessage("Flashlight is not avilable on this device.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
});
}
#Override
protected void onStop() {
super.onStop();
if (camera != null){
camera.release();
camera = null;
}
}
}

From Android 6.0 (Marshmallow) Android requires that you ask permission by code at runtime or at the time when the feature are gonna be used.
There are more about here with examples. https://developer.android.com/training/permissions/requesting.html
Anything below Android 6.0 will just require as you already have done it in the manifest

Related

My Android TV app only works on Android TV emulator (Android Studio)

My Android TV app only works on Android TV Emulator (Android Studio).
When I install the application on my tv box, the application crashes
here is my Androidmanifest and my activity
Androidmanifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.tvonhd.uniplaytvbox">
<uses-feature
android:name="android.software.leanback"
android:required="true" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.ErrorActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.VideoDetailsActivity2" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.VideoDetailsActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.SignUpActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.PlayerActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.LoginActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.ItemGenreActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.ItemCountryActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.ActivationActivity" />
<activity android:name="com.tvonhd.uniplaytvbox.ui.activity.SearchActivity" />
<activity
android:name="com.tvonhd.uniplaytvbox.ui.activity.LeanbackActivity"
android:theme="#style/AppTheme" />
<activity
android:name="com.tvonhd.uniplaytvbox.ui.activity.SplashScreenActivity"
android:banner="#drawable/ic_launcher_tv"
android:icon="#drawable/ic_launcher_tv"
android:roundIcon="#drawable/ic_launcher_tv"
android:logo="#drawable/ic_launcher_tv">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.tvonhd.uniplaytvbox.ui.activity.LoginChooserActivity"
android:screenOrientation="fullSensor"
android:windowSoftInputMode="stateHidden|adjustResize" />
<activity android:name="com.tvonhd.uniplaytvbox.MainActivity" />
</application>
</manifest>
here is my activity
activity:
```
package com.tvonhd.uniplaytvbox.fragments;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.fragment.app.FragmentManager;
import androidx.leanback.app.RowsSupportFragment;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.HeaderItem;
import androidx.leanback.widget.ListRow;
import androidx.leanback.widget.ListRowPresenter;
import androidx.leanback.widget.OnItemViewClickedListener;
import androidx.leanback.widget.OnItemViewSelectedListener;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.Row;
import androidx.leanback.widget.RowPresenter;
import com.tvonhd.uniplaytvbox.Config;
import com.tvonhd.uniplaytvbox.NetworkInst;
import com.tvonhd.uniplaytvbox.utils.PreferenceUtils;
import com.tvonhd.uniplaytvbox.R;
import com.tvonhd.uniplaytvbox.database.DatabaseHelper;
import com.tvonhd.uniplaytvbox.model.Channel;
import com.tvonhd.uniplaytvbox.model.HomeContent;
import com.tvonhd.uniplaytvbox.model.VideoContent;
import com.tvonhd.uniplaytvbox.network.RetrofitClient;
import com.tvonhd.uniplaytvbox.network.api.HomeApi;
import com.tvonhd.uniplaytvbox.ui.BackgroundHelper;
import com.tvonhd.uniplaytvbox.ui.activity.ErrorActivity;
import com.tvonhd.uniplaytvbox.ui.activity.LeanbackActivity;
import com.tvonhd.uniplaytvbox.ui.activity.PlayerActivity;
import com.tvonhd.uniplaytvbox.ui.activity.VideoDetailsActivity;
import com.tvonhd.uniplaytvbox.ui.presenter.CardPresenter;
import com.tvonhd.uniplaytvbox.ui.presenter.SliderCardPresenter;
import com.tvonhd.uniplaytvbox.ui.presenter.TvPresenter;
import com.tvonhd.uniplaytvbox.utils.PaidDialog;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class HomeFragment extends RowsSupportFragment {
private BackgroundHelper bgHelper;
private ArrayObjectAdapter rowsAdapter;
private CardPresenter cardPresenter;
private View v;
private LeanbackActivity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bgHelper = new BackgroundHelper(getActivity());
activity = (LeanbackActivity) getActivity();
activity.showLogo();
setOnItemViewClickedListener(getDefaultItemViewClickedListener());
setOnItemViewSelectedListener(getDefaultItemSelectedListener());
if (new NetworkInst(activity).isNetworkAvailable()) {
loadHomeContent();
} else {
Intent intent = new Intent(activity, ErrorActivity.class);
startActivity(intent);
activity.finish();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = super.onCreateView(inflater, container, savedInstanceState);
return v;
}
private void loadHomeContent() {
final SpinnerFragment mSpinnerFragment = new SpinnerFragment();
final FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.custom_frame_layout, mSpinnerFragment).commit();
Retrofit retrofit = RetrofitClient.getRetrofitInstance();
HomeApi api = retrofit.create(HomeApi.class);
Call<List<HomeContent>> call = api.getHomeContent(Config.API_KEY);
call.enqueue(new Callback<List<HomeContent>>() {
#Override
public void onResponse(Call<List<HomeContent>> call, Response<List<HomeContent>> response) {
if (response.isSuccessful()) {
List<HomeContent> homeContents = response.body();
//Log.d("size:", homeContents.size()+"");
if (homeContents.size() > 0) {
loadRows(homeContents);
} else {
Toast.makeText(getContext(), getResources().getString(R.string.no_data_found), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), response.errorBody().toString(), Toast.LENGTH_SHORT).show();
}
// hide the spinner
fm.beginTransaction().remove(mSpinnerFragment).commitAllowingStateLoss();
}
#Override
public void onFailure(Call<List<HomeContent>> call, Throwable t) {
t.printStackTrace();
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
// hide the spinner
getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit();
}
});
}
private void loadRows(List<HomeContent> homeContents) {
rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
cardPresenter = new CardPresenter();
SliderCardPresenter sliderCardPresenter = new SliderCardPresenter();
TvPresenter tvPresenter = new TvPresenter();
int i;
for (i = 0; i < homeContents.size(); i++) {
ArrayObjectAdapter listRowAdapter;
HeaderItem header;
if (i == 0) {
// load slider
listRowAdapter = new ArrayObjectAdapter(sliderCardPresenter);
header = new HeaderItem(i, "");
} else if (i == 1) {
//load tv layout
listRowAdapter = new ArrayObjectAdapter(tvPresenter);
header = new HeaderItem(i, homeContents.get(i).getTitle());
} else {
listRowAdapter = new ArrayObjectAdapter(cardPresenter);
header = new HeaderItem(i, homeContents.get(i).getTitle());
}
//for (int j = 0; j < NUM_COLS; j++) {
for (int j = 0; j < homeContents.get(i).getContent().size(); j++) {
VideoContent videoContent = homeContents.get(i).getContent().get(j);
if (homeContents.get(i).getType().equalsIgnoreCase("tv")) {
videoContent.setType("tv");
} else if (homeContents.get(i).getType().equalsIgnoreCase("movie")) {
videoContent.setType("movie");
} else if (homeContents.get(i).getType().equalsIgnoreCase("tvseries")) {
videoContent.setType("tvseries");
} else if (homeContents.get(i).getType().equalsIgnoreCase("slider")) {
if (videoContent.getIsTvseries().equals("1")) {
videoContent.setType("tvseries");
} else if (videoContent.getIsTvseries().equals("0")) {
videoContent.setType("movie");
}
}
listRowAdapter.add(videoContent);
}
rowsAdapter.add(new ListRow(header, listRowAdapter));
}
setAdapter(rowsAdapter);
setCustomPadding();
}
private void setCustomPadding() {
// getView().setPadding(Utils.dpToPx(-24, getContext()), Utils.dpToPx(70, getContext()), 0, 0);
}
// click listener
private OnItemViewClickedListener getDefaultItemViewClickedListener() {
return new OnItemViewClickedListener() {
#Override
public void onItemClicked(Presenter.ViewHolder viewHolder, Object o,
RowPresenter.ViewHolder viewHolder2, Row row) {
VideoContent videoContent = (VideoContent) o;
String status = new DatabaseHelper(getContext()).getActiveStatusData().getStatus();
if (videoContent.getType().equals("tv")) {
if (videoContent.getIsPaid().equals("1")) {
if (PreferenceUtils.isValid(getActivity())) {
if (status.equals("active")) {
Intent intent = new Intent(getActivity(), PlayerActivity.class);
intent.putExtra("id", videoContent.getId());
intent.putExtra("videoType", videoContent.getStreamFrom());
intent.putExtra("streamUrl", videoContent.getStreamUrl());
startActivity(intent);
} else {
//subscription is not active
//new PaidDialog(getActivity()).showPaidContentAlertDialog();
PaidDialog dialog = new PaidDialog(getContext());
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.show();
}
} else {
//saved data is not valid, because it was saved more than 2 hours ago
PreferenceUtils.updateSubscriptionStatus(getActivity());
}
} else {
Intent intent = new Intent(getActivity(), PlayerActivity.class);
intent.putExtra("id", videoContent.getId());
intent.putExtra("videoType", videoContent.getStreamFrom());
intent.putExtra("streamUrl", videoContent.getStreamUrl());
startActivity(intent);
}
} else {
Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
intent.putExtra("id", videoContent.getId());
intent.putExtra("type", videoContent.getType());
intent.putExtra("thumbImage", videoContent.getThumbnailUrl());
startActivity(intent);
}
}
};
}
//listener for setting blur background each time when the item will select.
protected OnItemViewSelectedListener getDefaultItemSelectedListener() {
return new OnItemViewSelectedListener() {
public void onItemSelected(Presenter.ViewHolder itemViewHolder, final Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof VideoContent) {
bgHelper = new BackgroundHelper(getActivity());
bgHelper.prepareBackgroundManager();
bgHelper.startBackgroundTimer(((VideoContent) item).getPosterUrl());
} else if (item instanceof Channel) {
bgHelper = new BackgroundHelper(getActivity());
bgHelper.prepareBackgroundManager();
bgHelper.startBackgroundTimer(((Channel) item).getPosterUrl());
}
}
};
}
}
My Android TV app only works on Android TV Emulator (Android Studio). When I install the application on my TV Box, the application crashes. this error is caused by leanback?
Thanks for your help
Make android.software.leanback feature required false.
<uses-feature
android:name="android.software.leanback"
android:required="false" />
If you set the required attribute value to true, your app will run only on devices that use the Leanback UI.

Map on phone is blank

Am trying to implement a google map on my app that has other activities but it is not working. When i compile the apk and run it in my phone the map appears blank anybody with an idea.
I tried to check my API key but it appears to be okay. I have been working on this for 3days any help?
This are my files
androidmanifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="#mipmap/kaps"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".models.StartWatchActivity"
android:label="#string/title_activity_start_watch" >
</activity>
<activity
android:name=".models.PaymentActivity"
android:label="#string/title_activity_payment" >
</activity>
<activity
android:name=".models.LocationActivity"
android:label="#string/title_activity_location" >
</activity>
<activity
android:name=".models.GetCurrentLocation"
android:label="#string/title_activity_get_current_location" >
</activity>
<activity
android:name=".models.ConfirmRegistration"
android:label="#string/title_activity_confirm_registration" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="*******************************" />
<activity
android:name=".models.MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
</application>
MapActivity.java
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.w3c.dom.Text;
import java.io.IOException;
import java.util.List;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
public void onSearch(View view){
EditText location_tf=(EditText)findViewById(R.id.TFaddress);
String location=location_tf.getText().toString();
List<Address> addressList=null;
if(location !=null ||!location.equals(""))
{
Geocoder geocoder=new Geocoder(this);
try {
addressList=geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address=addressList.get(0);
LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
public void onZoom(View view)
{
if (view.getId()==R.id.Bzoomin)
{
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
if (view.getId()==R.id.Bzoomout)
{
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
}
public void changeType(View view)
{
if(mMap.getMapType()==GoogleMap.MAP_TYPE_NORMAL)
{
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMyLocationEnabled(true);
}
}
locationactivity.java
when a button is clicked takes you to the MapActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.example.barbegambino.parkcar.R;
public class LocationActivity extends AppCompatActivity {
Button button4,button,button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
button4 = (Button) findViewById(R.id.button4);
button = (Button) findViewById(R.id.button);
button5 = (Button) findViewById(R.id.button5);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LocationActivity.this, GetCurrentLocation.class);
startActivity(intent);
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LocationActivity.this, StartWatchActivity.class);
startActivity(intent);
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(LocationActivity.this,MapsActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_location, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Your problem occurs due to missing of Google Play Service.
3 step process:-
4.4 Kitkat
5.0 Lollipop
5.1 Lollipop
6.0 Marshmallow
7.0 Nougat
7.1 Nougat (webview patch)
Download from above link
Just drag & drop downloaded zip file to genymotion and restart
Add google account and download Google Play Service and Run.
Note :- The above process can be automated if you install OpenGapps apk in your device.
Download link for apk

Get and redirect URL from result of scan QR code

I'm a new android.
I have a simple application for scan QR code. But it only show result.
My picture
I want it get url from this result and redirect to it
This is my code.
MainActivity.java
package app.num.barcodescannerproject;
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void QrScanner(View view){
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.e("handler", rawResult.getText());
Log.e("handler", rawResult.getBarcodeFormat().toString());
// show the scanner result into dialog box.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Result");
builder.setMessage(rawResult.getText());
AlertDialog alert1 = builder.create();
alert1.show();
mScannerView.resumeCameraPreview(this);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.num.barcodescannerproject">
<uses-permission android:name="android.permission.CAMERA" />
<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>
Please help me. Thank all

Cannot redirect to next page

Here is my Home.java code. It is not redirecting to next page, even if I change the intent to (home.this, MainActivity.class):
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Home extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Thread timer = new Thread() {
public void run() {
try{
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMainActivity = new Intent("com.droid.mine.MainActivity");
startActivity(openMainActivity);
}
}
};
timer.start();}{
}
}
Here is my MainActivity.java code. I.e next page
I am getting the error as ClassCastException:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button log,sign;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
log = (Button) findViewById(R.id.register);
sign = (Button) findViewById(R.id.linktologin);
log.setOnClickListener((OnClickListener) this);
sign.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.register:
break;
case R.id.linktologin:
Intent i = new Intent();
i.setClass(MainActivity.this,Register.class);
startActivity(i);
break;
}
}
}
Manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.mine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.droid.mine.Home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name=".MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the logcat error:
04-21 15:59:07.156: I/ApplicationPackageManager(21341): cscCountry is not German : INS
04-21 15:59:07.273: D/dalvikvm(21341): GC_EXTERNAL_ALLOC freed 78K, 47% free 2860K/5379K, external 408K/517K, paused 95ms
04-21 15:59:09.648: W/dalvikvm(21341): threadid=9: thread exiting with uncaught exception (group=0x40018578)
04-21 15:59:09.710: E/AndroidRuntime(21341): FATAL EXCEPTION: Thread-10
04-21 15:59:09.710: E/AndroidRuntime(21341): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.droid.mine.MainActivity }
04-21 15:59:09.710: E/AndroidRuntime(21341): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
04-21 15:59:09.710: E/AndroidRuntime(21341): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
04-21 15:59:09.710: E/AndroidRuntime(21341): at android.app.Activity.startActivityForResult(Activity.java:2827)
04-21 15:59:09.710: E/AndroidRuntime(21341): at android.app.Activity.startActivity(Activity.java:2933)
04-21 15:59:09.710: E/AndroidRuntime(21341): at com.droid.mine.Home$1.run(Home.java:21)
Here is my Register.java class
package com.droid.mine;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Register extends Activity implements View.OnClickListener {
Button backlog,regg;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
backlog = (Button) findViewById(R.id.loginn);
regg = (Button) findViewById(R.id.signup);
backlog.setOnClickListener(this);
}
public void onClick (View v)
{
switch(v.getId()) {
case R.id.loginn:
break;
case R.id.signup:
Intent in = new Intent();
in.setClass(Register.this,MainActivity.class);
startActivity(in);
break;
}
}
}
Firstly
Change where you intent with this:
Intent intent = new Intent(Home.this,MainActivity.class);
startActivity(intent);
Secondly
Just update your manifest with this:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.droid.mine.Home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.droid.mine.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.droid.mine.Register"
android:label="#string/app_name" >
</activity>
</application>
Use this as home activity
package com.example.pms;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.widget.Toast;
public class SplashScreen extends Activity {
/*
* The thread to process splash screen events
*/
private Thread mSplashThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// splash screen view
setContentView(R.layout.activity_splash);
final SplashScreen mSplashScreen=this;
/*
* The thread to wait for splash screen events
*/
mSplashThread =new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(3000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
startActivity(new Intent(getBaseContext(), MainActivity.class));
new Runnable() {
#Override
public void run() {
mSplashThread.stop();
}
};
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
you can use this code instead
replace
.MainActivity
with
com.droid.mine.MainActivity
while declaring in Manifest file.
//delay in ms
int DELAY = 1000;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(Home.this,MainActivity.class);
startActivity(intent);
}
}, DELAY);
Change your Intent to:
Intent openMainActivity = new Intent(Home.this, MainActivity.class);
startActivity(openMainActivity);
and in manifest file use:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testapp.Home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
You need to register MainActivity and Register Activity in the manifest:
<activity android:name=".Mainactivity" />
<activity android:name=".Register" />
log.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
});
}
sign.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent();
i.setClass(MainActivity.this,Register.class);
startActivity(i);
});
}
I hope this works:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Home extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Thread splashTimer = new Thread() {
public void run() {
try {
int splashTimer = 0;
while (splashTimer < 2000) {
sleep(100);
splashTimer = splashTimer + 100;
}
Intent intent;
intent = new Intent(Home.this, MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d("asd","dasd");
}
finally {
finish();
}
}
};
splashTimer.start();
}
}
Edit: Replace your manifest file with this, now I hope it works:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.mine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.droid.mine.Home"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.droid.mine.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>

how to turn the flash on the phone (flashlight)?

There is a code that is to turn on the flash when you press the button, this is why it is not happening. Help me understand why this is not happening, please. Tested the application on samsung galaxy nexus and lg p970.
Activity:
package com.example.Flashlight;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
Camera camera;
boolean isFlashOn;
boolean hasFlash;
Parameters params;
Button flashlightButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkFlashlight();
getCamera();
initFlashlightButton();
}
void checkFlashlight() {
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(
MainActivity.this).create(); alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
return;
}
}
// initialization button and click listener
private void initFlashlightButton() {
flashlightButton = (Button) findViewById(R.id.flash_light);
flashlightButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isFlashOn) turnOffFlash();
else turnOnFlash();
}
});
}
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// here must be of the flash (it is not clear why this is not happening)
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) return;
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
// flash OFF
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) return;
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent">
<Button
android:id="#+id/flash_light"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Flashlight"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Flashlight"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk android:minSdkVersion="8"/>
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
To turn on the Camera :
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
To turn off the Camera :
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
Also you'll need to put that in your manifest :
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Try adding this to your manifest:
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

Categories

Resources