토글 버튼이있는 애플리케이션이 있습니다. 이 토글 버튼의 기능은 플래시를 켰을 때 켜고 플래시를 다시 누르면 다시 깜박입니다. 그러나 장치가 잠겨 있거나 잠자고 있거나 작동 중일 때도이 기능을 원합니다. 전원 버튼을 두 번 클릭 할 때마다 앱 기능이 서비스에서 실행되고 브로드 캐스트를 통해 플래시가 켜져 야합니다. 친절하게이 일을 어떻게 할 수 있는지 알려주십시오.전원 버튼을 두 번 클릭하면 플래시가 켜져 야합니다.
-1
A
답변
-1
MainActivity
package com.example.salal_khan.flashlightservices;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
startService();
}
public void startService() {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService() {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
서비스
package com.example.salal_khan.flashlightservices;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by salal-khan on 12/1/2017.
*/
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
final IntentFilter filter = new
IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
}
방송 수신기
package com.example.salal_khan.flashlightservices;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.util.Log;
import android.hardware.Camera.Parameters;
/**
* Created by salal-khan on 12/1/2017.
*/
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
Camera camera;
boolean isON = false;
int number_of_clicks = 0;
boolean thread_started = false;
final int DELAY_BETWEEN_CLICKS_IN_MILLISECONDS = 550;
@Override
public void onReceive(final Context context, final Intent intent) {
++number_of_clicks;
if (!thread_started) {
new Thread(new Runnable() {
@Override
public void run() {
thread_started = true;
try {
Thread.sleep(DELAY_BETWEEN_CLICKS_IN_MILLISECONDS);
if (number_of_clicks == 1) {
} else if (number_of_clicks == 2) {
if (isON) {
if (camera == null) {
camera = Camera.open();
}
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isON = false;
} else {
if (camera == null) {
camera = Camera.open();
}
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isON = true;
}
}
number_of_clicks = 0;
thread_started = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Log.e("LOB", "onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
wasScreenOn = true;
}
else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.e("LOB", "userpresent");
Log.e("LOB", "wasScreenOn" + wasScreenOn);
}
}
}
매니페스트
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salal_khan.flashlightservices">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<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>
<service android:name=".MyService" />
</application>
</manifest>
0
이미지 캡처
package com.example.salal_khan.SecuriteCameraServices;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.util.Log;
import android.hardware.Camera.Parameters;
import android.view.SurfaceView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by salal-khan on 12/1/2017.
*/
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
Camera camera = null;
boolean isON = false;
int number_of_clicks = 0;
boolean thread_started = false;
final int DELAY_BETWEEN_CLICKS_IN_MILLISECONDS = 550;
Context contexts;
@Override
public void onReceive(final Context context, final Intent intent) {
contexts = context;
++number_of_clicks;
final SurfaceView mview = new SurfaceView(context);
if (camera==null) {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
}
if (!thread_started) {
new Thread(new Runnable() {
@Override
public void run() {
thread_started = true;
try {
Thread.sleep(DELAY_BETWEEN_CLICKS_IN_MILLISECONDS);
if (number_of_clicks == 1) {
} else if (number_of_clicks == 2) {
if (isON) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
try {
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null, null, photoCallback);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} else {
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
try {
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null, null, photoCallback);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
number_of_clicks = 0;
thread_started = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Log.e("LOB", "onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.e("LOB", "userpresent");
Log.e("LOB", "wasScreenOn" + wasScreenOn);
}
}
Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Uri uriTarget = contexts.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = contexts.getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
// Toast.makeText(contexts.getApplicationContext(), "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
매니페스트
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salal_khan.SecuriteCameraServices">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:name=".MyApplication"
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="com.example.salal_khan.SecuriteCameraServices.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.salal_khan.SecuriteCameraServices.MyService" />
</application>
</manifest>
관련 문제
- 1. 두 번 전원 버튼을 누르면 SMS가 전송됩니다.
- 2. 전원 버튼을 클릭하면 응용 프로그램 활성화
- 3. 버튼을 두 번 클릭하면 무언가가 발생합니다
- 4. 안드로이드 버튼을 두 번 클릭하면 작동합니다.
- 5. 로그인 버튼을 두 번 클릭하면 ParseUser로 로그인합니다.
- 6. 두 번 클릭하면 이벤트가 두 번 발생합니다.
- 7. Android 용 Workarround 6.0.1 전원 버튼을 두 번 누르기
- 8. 버튼을 3 번 클릭하면 동작합니다.
- 9. 버튼을 한 번 클릭하면 버튼을 추가하는 방법
- 10. 안드로이드 - 검색 가능한 액티비티, 트랜잭션이 두 번 뒤로 눌러져 야합니다.
- 11. 버튼을 두 번 클릭하면 양식을 열지 마십시오 (double)
- 12. Android 버튼을 두 번 클릭하면 클릭 이벤트가 발생합니다.
- 13. asp.net 버튼을 두 번 클릭하면 CSS 클래스 적용
- 14. 제출 버튼을 두 번 이상 클릭하면 값이 사라집니다.
- 15. 이벤트를 두 번 이상 처리하는 버튼을 클릭하면 setInterval이 이상하게 들립니까?
- 16. 내 IOS에서 버튼을 클릭하면 이벤트가 두 번 발생합니다. 앱
- 17. Phonegap (코르도바). Return 버튼을 두 번 클릭하면 앱이 깨집니다.
- 18. AS3에서 마우스 오른쪽 버튼을 두 번 클릭하면 어떻게됩니까?
- 19. 버튼을 두 번 누르십시오.
- 20. 두 번 클릭하면 링크가 전환됩니다.
- 21. 두 번 클릭하면 링크가 열립니다.
- 22. NSTimer가 두 번 클릭하면 멈추지 않습니다.
- 23. 전원 메뉴에서 전원 끄기 소프트 버튼을 제거하십시오.
- 24. 왜 버튼을 클릭하면 여러 번 발사됩니까?
- 25. Angular2 div를 바깥 쪽을 클릭하면 가까워 야합니다
- 26. 버튼을 두 번째 클릭하면 멈 춥니 다.
- 27. 버튼을 여러 번 클릭하면 Android에서 button.setOnClickListener가 여러 번 호출됩니다.
- 28. 버튼을 두 번 이상 클릭하십시오
- 29. AlertDialog 버튼을 두 번 클릭해야합니다.
- 30. Android 버튼은 두 번 클릭하면 작동합니다.