I'm trying to make a Flahlight app, but when I press the "Flashlight" button to turn the flashlight on, the app crashes.
Here's my code:
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" >
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<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.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.flashlight.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>
</manifest>
java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void toggleFlashlight() {
Camera cam;
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
}
I put the codes in images because I couldn't get the code block to work.
Add parameter View v to public void toggleFlashlight()
as
public void toggleFlashlight(View v)
Change:
public void toggleFlashlight() {
Camera cam;
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
to
public void toggleFlashlight(View v) {
Camera cam;
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
From your logcat, it is seen that you're setting the method for onClick in your XML to toggleFlashlight(). Due to being a method called on a View's click, it must match the signature of other onClick() methods, and have a View parameter.
private void switchOn()
{
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
if(cam == null)
{
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
}
}
private void switchOff()
{
if(cam != null)
{
cam.stopPreview();
cam.release();
cam = null;
}
}
Related
new to android programming. I am trying to create a very simple flashlight app in android studio and i want to be able to run the app when the phone is locked and as a background activity. I have understood that i should use Service but i can't figure out how or where to implement it in my code.
Thank you.
MainActivity:
public class MainActivity extends Activity {
//flag to detect flash is on or off
private boolean isLightOn = false;
private Camera camera;
private ImageButton button;
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton) findViewById(R.id.imageButton);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return;
}
camera = open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (isLightOn) {
p.setFlashMode(FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLightOn = false;
button.setImageResource(R.drawable.onbuttontrans);
} else {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
button.setImageResource(R.drawable.offbuttontrans);
isLightOn = true;
}
}
});
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.g131146.flashlight" >
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application android:theme="#android:style/Theme.Black.NoTitleBar"
android:icon="#drawable/appicon">
<activity
android:label="Flashlight"
android:name=".MainActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I was facing this similar issue and solved it through the following method.
Here we basically combined the code in the onPause and onStop methods. Hence you could comment out the code in there. I didn't really use any Service as this fixed the issue. Hope it helps!
Add this in your MainActivity:
#Override
public void onBackPressed() {
super.onBackPressed();
myParameters = myCamera.getParameters();
myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
myCamera.setParameters(myParameters);
myCamera.stopPreview();
FlashOn = false;//this is a boolean
if (myCamera != null) {
myCamera.release();
myCamera = null;
}
}
I am not able to figure out what am I writing wrong. This seems to a very simple case but I am not able to make this work.
1. registerReceiver is returning null, does that matters?
2. receiver's onReceive not getting called - is this linked to registerReceiver
giving null.
Below is code inside my activity
WifiManager mainWifiObj;
WifiScanReceiver wifiReceiver;
String wifis[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
final Button button = (Button) findViewById(R.id.nextButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mainWifiObj.startScan();
}
});
}
protected void onStop() {
unregisterReceiver(wifiReceiver);
super.onStop();
}
protected void onStart() {
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
//registerReceiver returning null
super.onStart();
}
class WifiScanReceiver extends BroadcastReceiver {
#SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
Log.v(this.getClass().getName(), "wifiScanList.size() : "
+ wifiScanList.size());
for (int i = 0; i < wifiScanList.size(); i++) {
wifis[i] = ((wifiScanList.get(i)).toString());
Log.v(this.getClass().getName(), wifiScanList.get(i).SSID);
}
}
}
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2e1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstStateActivity"
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>
</manifest>
I took reference from http://www.tutorialspoint.com/android/android_wi_fi.htm
and tried reproducing similar code but it dint worked.
I am running this in Android emulator for Windows.
im trying to make basic beacon application in android studio. I just want to scan beacons and list them into the screen. Here are my codes. I took them from somewhere.
public class MainActivity extends ActionBarActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
#Override public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
Log.d("TAG", "Ranged beacons: " + beacons);
}
});
}
#Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG","Cannot start ranging", e);
}
}
});
}
#Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG", "Cannot stop but it does not matter now", e);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.disconnect();
}
}
Here is my manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oem.estimote_ibeacon_app" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<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=".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>
<service
android:name="com.estimote.sdk.service.BeaconService"
android:exported="false"/>
</application>
</manifest>
When i opened application it says "stopped" please help. I have beacons for test. Where is my mistake? Thank you.
it seems that ALL_ESTIMOTE_BEACONS in
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
is null.
enter a valid regionid instead of "regionid" in
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);
i am new to android. i want to make an app from where i can take pic using camera object. here is my simple code
public class Main extends Activity {
Camera camera;
ImageView iv;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = camera.open();
b = (Button) findViewById(R.id.bStartCamera);
iv = (ImageView) findViewById(R.id.imageView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (camera != null) {
camera.takePicture(null, null, new CallBack());
} else {
Log.d("", "Camera Object is Null");
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
}
class CallBack implements PictureCallback {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("", "In the Callback Method");
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.homescreensetter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.homescreensetter.Main"
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>
</manifest>
i tested this code on my device which have a back camera but the call back method onPictureTaken was never called when i take picture. please any kind of help will be much appriciated,Thanks
was trying to implement an service that disables bluetooth and wifi when ever user tries to enable bluetooth and Wifi as they are few ways of getting infected.
i have disable use of application use of app locker.
this is what i tried with the tutorials available but so far i have failed to run it on the bootup
Service class
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
BroadcastReceiver mReceiver = new StatusReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
}
Reciver
public class StatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
wifiManager.setWifiEnabled(false);
}
}
}
Activity
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("HI","I am on activity");
startService(new Intent(this,MyService.class));
finish();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.active"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" >
</service>
<receiver android:name=".StatusReceiver" >
</receiver>
</application>
</manifest>