Flashlight Widget帮助Android Studio

时间:2016-09-28 17:14:34

标签: java android

所以我刚刚开始使用widget手电筒应用程序,我坚持让我的小部件工作。

这是我的flashwidget.java

package layout;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.RemoteViews;

import com.example.lipa.flashlight.R;   

public class flashwidget extends AppWidgetProvider {

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flashwidget);
if (isFlashOn) {
// Flash light active
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.switchon);
views.setImageViewBitmap(R.id.switchonw, bitmap);

// Create the intent, set the action
Intent underlyingIntent = new Intent(context, FlashlightIntentService.class);
underlyingIntent.setAction(FlashlightIntentService.FLASH_MODE_OFF);

// Create the pending intent
PendingIntent turnOffIntent = PendingIntent.getService(context, 1,
underlyingIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.switchonw, turnOffIntent);

} else {
// Flash light off
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.buttonoff);
views.setImageViewBitmap(R.id.switchonw, bitmap);

// Create the intent, set the action
Intent underlyingIntent = new Intent(context, FlashlightIntentService.class);
            underlyingIntent.setAction(FlashlightIntentService.FLASH_MODE_TORCH);

// Create the pending intent
PendingIntent turnOnIntent = PendingIntent.getService(context, 1,
underlyingIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.switchonw, turnOnIntent);
}

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

MainActivity.java

package com.example.lipa.flashlight;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.widget.Toast;

import com.google.android.gms.ads.MobileAds;    
import java.security.Permission;    
import layout.FlashlightIntentService;

public class MainActivity extends Activity {

ImageButton btnSwitch;
private Camera camera;
private boolean isFlashOn;
private AdView mAdView;
private boolean hasFlash;
Parameters params;    

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");

mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// flash switch button
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);    

// Provjera za kameru
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

if (!hasFlash) {
// uredaj ne podrzava kameru
// Pokazi poruku i zatvori aplikaciju
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) {
// zatvaranje aplikacije
finish();
}
});
alert.show();
return;
}

// dohvati kameru
getCamera();

// prikazuje sliku switcha
toggleButtonImage();    

// Pali i gasi flash
btnSwitch.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}    

// Dohvaca kameru
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error.", e.getMessage());
}
}
}

// Paljenje flasha
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;

// changing button/switch image
toggleButtonImage();
}    
}

// Turning Off flash
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;

// changing button/switch image
toggleButtonImage();
}
}

/*
* Toggle switch button images
* changing image states to on / off
     * */
private void toggleButtonImage() {
if (isFlashOn) {
btnSwitch.setImageResource(R.drawable.buttonoff);
} else {
btnSwitch.setImageResource(R.drawable.switchon);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
protected void onPause() {
super.onPause();

// on pause turn off the flash
turnOffFlash();
}

@Override
protected void onRestart() {
super.onRestart();
}

@Override
protected void onResume() {
super.onResume();

// on resume turn on the flash
if (hasFlash)
turnOnFlash();
}

@Override
protected void onStart() {
super.onStart();

// on starting the app get the camera params
getCamera();
}

@Override
protected void onStop() {
super.onStop();

// on stop release the camera

if (camera != null) {
camera.release();
camera = null;
}
}
}   

0 个答案:

没有答案