I'm developing music player notification with progress bar . Somehow I managed to show progress with Handler but now the problem is after some time whole android system freezes. And then rebooting the system is only a solution.
I'm new in android and this is my first app, please help me to complete my first app. I will be very thankful to you.
Here is Screenshot of Music Player Notification with Progress Bar
I've attached screenshot of notification and here is my code. These codes are running in media player foreground service
Some global variables:
private static RemoteViews views;
private static RemoteViews bigViews;
private static Notification status;
private static final int NOTIFICATION_ID = 101;
public Handler mSeekbarUpdateHandler= new Handler();
This is Runnable code:
public Runnable mUpdateseekbar=new Runnable() {
#Override
public void run() {
if(mediaPlayer != null) {
bigViews.setProgressBar(R.id.progressBar, mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), false);
bigViews.setTextViewText(R.id.time_start, timeConvert(mediaPlayer.getCurrentPosition()));
}
startForeground(NOTIFICATION_ID, status);
mSeekbarUpdateHandler.postDelayed(this,1000);
}};
Now calling handler:
#Override
public void onPlay() {
super.onPlay();
resumeMedia();
buildNotification(PlaybackStatus.PLAYING);
mSeekbarUpdateHandler.postDelayed(mUpdateseekbar,0);
//also calling this Handler from method onSkipToNext() and previous. it means when changing song also calling Handler each time.
Finally build notification code:
private void buildNotification(PlaybackStatus playbackStatus)
{
//This section containing long code which works fine so no need to make post longer.
}
when running this code after few minute system becomes slow and finally freeze.
After removing Handler no problem occurs in notification but my progress bar not updating.
}
I have successfully solved this issue by every time Re-building whole notification in the Runnable ...
I have replaced above Runnable code with the following code...
public Runnable mUpdateseekbar = new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying()) {
buildNotification(PlaybackStatus.PLAYING);
}
mSeekbarUpdateHandler.postDelayed(this, 1000);
}
};
And here's the buildNotification method..
private void buildNotification(PlaybackStatus playbackStatus) {
RemoteViews views;
RemoteViews bigViews;
Notification status;
try {
int notificationAction = android.R.drawable.ic_media_pause;
PendingIntent play_pauseAction = null;
if (playbackStatus == PlaybackStatus.PLAYING) {
notificationAction = R.drawable.noty_pause;
play_pauseAction = playbackAction(1);
} else if (playbackStatus == PlaybackStatus.PAUSED) {
notificationAction = R.drawable.noty_play;
play_pauseAction = playbackAction(0);
}
views = new RemoteViews(getPackageName(),
R.layout.status_bar);
bigViews = new RemoteViews(getPackageName(),
R.layout.status_bar_expanded);
views.setImageViewBitmap(R.id.status_bar_album_art,
largeIcon);
bigViews.setImageViewBitmap(R.id.status_bar_album_art,
largeIcon);
views.setImageViewResource(R.id.status_bar_playPause,
notificationAction);
bigViews.setImageViewResource(R.id.status_bar_play,
notificationAction);
bigViews.setProgressBar(R.id.progressBar, mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), false);
views.setTextViewText(R.id.status_bar_track_name, activeAudio.getTitle());
bigViews.setTextViewText(R.id.status_bar_track_name, activeAudio.getTitle());
views.setTextViewText(R.id.status_bar_artist_name, activeAudio.getAlbum() + "-" + activeAudio.getArtist());
bigViews.setTextViewText(R.id.status_bar_artist_name, activeAudio.getAlbum() + "-" + activeAudio.getArtist());
views.setTextViewText(R.id.status_bar_metadata, displayMeta);
bigViews.setTextViewText(R.id.status_bar_album_name, displayMeta);
bigViews.setTextViewText(R.id.track_num, (audioIndex + 1) + "/" + audioList.size());
bigViews.setTextViewText(R.id.end_time, timeConvert(mediaPlayer.getDuration()));
bigViews.setTextViewText(R.id.time_start, timeConvert(mediaPlayer.getCurrentPosition()));
views.setOnClickPendingIntent(R.id.status_bar_playPause, play_pauseAction);
bigViews.setOnClickPendingIntent(R.id.status_bar_play, play_pauseAction);
views.setOnClickPendingIntent(R.id.status_bar_next, playbackAction(2));
bigViews.setOnClickPendingIntent(R.id.status_bar_next, playbackAction(2));
views.setOnClickPendingIntent(R.id.status_bar_previous, playbackAction(3));
bigViews.setOnClickPendingIntent(R.id.status_bar_prev, playbackAction(3));
bigViews.setOnClickPendingIntent(R.id.status_bar_ff, playbackAction(5));
bigViews.setOnClickPendingIntent(R.id.status_bar_rev, playbackAction(6));
bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, playbackAction(4));
views.setOnClickPendingIntent(R.id.status_stop,playbackAction(4));
status = new Notification.Builder(this).build();
status.contentView = views;
status.bigContentView = bigViews;
status.flags = Notification.FLAG_ONGOING_EVENT;
status.visibility = Notification.VISIBILITY_PUBLIC;
status.tickerText = activeAudio.getTitle();
if (playbackStatus==PlaybackStatus.PLAYING) {
status.icon = R.drawable.notyskeep;
} else {
status.icon = R.drawable.noty_small_blue;
}
status.priority = Notification.PRIORITY_MAX;
status.contentIntent = playbackAction(8);
startForeground(NOTIFICATION_ID, status);
} catch (Throwable e) {
Toast.makeText(getApplicationContext(), "Exception raised:" + e, Toast.LENGTH_LONG).show();
}
}
Related
Google has its clock app, which includes its stopwatch. I'm currently trying to create in my app a (count-up) timer, or you can call it a stopwatch, that will be able to run in the background, and when it runs in the background I want it to also show a notification, that displays the time it counts and a "Stop" button (all of this happens in google clock app (see here)). For the timer in my app, I'm using a Handler that posts a Runnable, which is posting itself. I'm writing my app in Java.
the code defining the 'timer' (Handler and Runnable):
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
seconds = (millis / 1000) + PrefUtil.getTimerSecondsPassed();
timerHandler.postDelayed(this, 500);
}
};
my onPause function:
#Override
public void onPause() {
super.onPause();
if (timerState == TimerState.Running) {
timerHandler.removeCallbacks(timerRunnable);
//TODO: start background timer, show notification
}
PrefUtil.setTimerSecondsPassed(seconds);
PrefUtil.setTimerState(timerState);
}
How can I implement the background service and the notification in my app?
Edit
I've managed to succeed in creating a foreground service that runs my timer, but I have two problems:
When I run the app after something like 5 minutes, the notification shows up in a 10-second delay.
the notification stops updating after around 30 seconds from the time it starts/resumes (The timer keeps running in the background, but the notification won't keep updating with the timer).
Here's my Services code:
public class TimerService extends Service {
Long startTime = 0L, seconds = 0L;
boolean notificationJustStarted = true;
Handler timerHandler = new Handler();
Runnable timerRunnable;
NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
public static final String TIMER_BROADCAST_ID = "TimerBroadcast";
Intent timerBroadcastIntent = new Intent(TIMER_BROADCAST_ID);
#Override
public void onCreate() {
Log.d(TAG, "onCreate: started service");
startForeground(1, new NotificationCompat.Builder(this, CHANNEL_ID).setSmallIcon(R.drawable.timer).setContentTitle("Goal In Progress").build());
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String goalName = intent.getStringExtra(PublicMethods.getAppContext().getString(R.string.timer_notification_service_current_goal_extra_name));
startTime = System.currentTimeMillis();
notificationJustStarted = true;
timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
seconds = (millis / 1000) + PrefUtil.getTimerSecondsPassed();
updateNotification(goalName, seconds);
timerHandler.postDelayed(this, 500);
}
};
timerHandler.postDelayed(timerRunnable, 0);
return START_STICKY;
}
public void updateNotification(String goalName, Long seconds) {
try {
if (notificationJustStarted) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
timerNotificationBuilder.setContentTitle("Goal In Progress")
.setOngoing(true)
.setSmallIcon(R.drawable.timer)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX);
notificationJustStarted = false;
}
timerNotificationBuilder.setContentText(goalName + " is in progress\nthis session's length: " + seconds);
startForeground(1, timerNotificationBuilder.build());
} catch (Exception e) {
Log.d(TAG, "updateNotification: Couldn't display a notification, due to:");
e.printStackTrace();
}
}
#Override
public void onDestroy() {
timerHandler.removeCallbacks(timerRunnable);
PrefUtil.setTimerSecondsPassed(seconds);
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
And here is how I start it in my fragment:
private void startTimerService() {
Intent serviceIntent = new Intent(getContext(), TimerService.class);
serviceIntent.putExtra(getString(R.string.timer_notification_service_current_goal_extra_name), "*Current Goal Name Here*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Objects.requireNonNull(getContext()).startForegroundService(serviceIntent);
}
}
UPDATE
When I run the app on google pixel emulator, I don't face any of the issues listed
There are 2 issues. I will try to solve both of them.
First issue
When I run the app after something like 5 minutes, the notification shows up in a 10-second delay.
For this, you need to update the notification with its code. Now, because it takes time to show up, show it in the activity where you start the service and then, pass the notification id to the service using its constructor. Using that id, update it in the service.
Let's hope that solves the first issue.
Second issue
the notification stops updating after around 30 seconds from the time it starts/resumes (The timer keeps running in the background, but the notification won't keep updating with the timer).
To solve that, you can clear the previous notification after 10 seconds by it's id. Then you can make a new random key for the notification( I'd prefer new Random().nextInt()) and then show it. But then you or anyone would say that there is so much sound when a notification comes. Just disable it this way when creating a channel:
notificationChannel.setSound(null, null);
NOTE: You might want to reinstall your app for it to work
If that seems complicated, see this:
Runnable running -> When 10 seconds done from previous notification display -> Clear the notification -> Make a new notification id -> show notification with that id -> Repeat
EDIT
This is the working code for me:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.util.concurrent.TimeUnit;
public class TimerService extends Service {
Long startTime = 0L, seconds = 0L;
boolean notificationJustStarted = true;
Handler timerHandler = new Handler();
Runnable timerRunnable;
private final String CHANNEL_ID = "Channel_id";
NotificationManager mNotificationManager;
NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID).setContentTitle(CHANNEL_ID);
#SuppressLint("InlinedApi")
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "created", Toast.LENGTH_SHORT).show();
String TAG = "Timer Service";
Log.d(TAG, "onCreate: started service");
startForeground(1, new NotificationCompat.Builder(TimerService.this, createChannel()).setContentTitle("Goal In Progress").setPriority(NotificationManager.IMPORTANCE_MAX).build());
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String goalName = "Sample Goal";
Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
startTime = System.currentTimeMillis();
notificationJustStarted = true;
timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
seconds = (millis / 1000) + PrefUtil.getTimerSecondsPassed(TimerService.this);
updateNotification(goalName, seconds);
Log.d("timerCount", seconds + "");
timerHandler.postDelayed(this, 1000);
}
};
timerHandler.postDelayed(timerRunnable, 0);
return Service.START_STICKY;
}
#SuppressLint("NewApi")
public void updateNotification(String goalName, long seconds) {
if (notificationJustStarted) {
Intent notificationIntent = new Intent(this, MainActivity.class);
#SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
timerNotificationBuilder.setContentTitle("Goal In Progress")
.setOngoing(true)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher_foreground);
notificationJustStarted = false;
}
long minutes = TimeUnit.SECONDS.toMinutes(seconds);
String time = minutes + ":" + (seconds - TimeUnit.MINUTES.toSeconds(minutes));
timerNotificationBuilder.setContentText(goalName + " is in progress\nthis session's length: " + time);
mNotificationManager.notify(1, timerNotificationBuilder.build());
startForeground(1, timerNotificationBuilder.build());
}
#Override
public void onDestroy() {
timerHandler.removeCallbacks(timerRunnable);
PrefUtil.setTimerSecondsPassed(this, seconds);
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#NonNull
#TargetApi(26)
private synchronized String createChannel() {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String name = "STOPWATCH";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setName("Notifications");
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
} else {
stopSelf();
}
return CHANNEL_ID;
}
}
You can also view my repo on this here. It is a complete stop watch app
I've found the reason why my notification stops updating after 30 seconds! Apparently,(according to this thread) on some devices running Android versions higher than 9 there are background restrictions.
These restrictions are the ones stopping my notifications from updating after 30 seconds from the moment the app gets closed, or in other words - from the moment they're becoming background activities (even though they are called through startForeground()).
There is no way around this setting. You cannot programmatically disable it. Your only option is to programmatically check if it's enabled using ActivityManager.isBackgroundRestricted() and display a pop-up informing your users on how to disable this setting
Says the user from the accepted answer in the thread.
And so, the issue of the notification not updating as expected is solved. The issue of the delay to show the first notification though remains unsolved, and there's another issue - every time the notification gets updated, the whole notification panel freezes for a second fraction.
I want to send value from string (distance to obstacle) to my TextView in main activity.
I tried to use Handler, but still not working (crash) or receive nothing.
A part code which receive data from HC-05 (screen where you see in debug value assignet to variable)
enter image description here
#Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while(true){
try {
bytes = inputStream.read(buffer);
final String comingMsg = new String(buffer,0,bytes);
Log.d(TAG,"InputStream: " + comingMsg);
/*mHandler2.post(new Runnable() {
#Override
public void run() {
Message message = new Message();
message.obj = comingMsg;
mHandler2.sendMessage(message);
}
});*/
}catch (IOException e){
Log.e(TAG,"Write: Error reading input." + e.getMessage());
active=false;
break;
}
}
}
Here It's parts of code from MainActivity where I tried put something to get values from service.
[I add, that for this moment i want to see something values from bluetooth in textView. Later I want to create parse string and send custom text to custom TextView - example: FL: (Front Left)- to one textView, FR: (Front Right) - to second textView]
There is method implementThreads(), because I wanted to do 6 Threads to 6 TextView which every time is refreshing value from string in Services (there I tried get value from Bluetooth Service)
Log.d(TAG,"Check intent - result");
if(getIntent().getIntExtra("result",0)==RESULT_OK){
mDevice = getIntent().getExtras().getParcelable("bonded device");
myBluetoothService = new MyBluetoothService(getApplicationContext());
startConnection(mDevice,MY_UUID);
Log.d(TAG,"Check is active service");
checkIfActive();
}
Log.d(TAG,"Check intent - connect_to_paired");
if(getIntent().getIntExtra("connect_to_paired",0)==RESULT_OK){
mDevice = getIntent().getExtras().getParcelable("bonded_paired_device");
myBluetoothService = new MyBluetoothService(getApplicationContext());
startConnection(mDevice,MY_UUID);
Log.d(TAG,"Check is active service");
checkIfActive();
}
}
#Override
public void onStart(){
super.onStart();
myBluetoothService = new MyBluetoothService(getApplicationContext());
}
public void checkIfActive(){
Log.d(TAG,"CheckIfActive: Started");
if(myBluetoothService.active){
Log.d(TAG,"CheckIfActive: Running method implementThreads()");
implementThreads();
}
}
public void implementThreads(){
Log.d(TAG,"ImplementThreads: Started");
Thread thread = new Thread(){
#Override
public void run() {
try{
sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
thread.start();
}
public void startConnection(BluetoothDevice device,UUID uuid){
Log.d(TAG,"StartConnection: Initializing connection");
myBluetoothService.startClient(device,uuid);
}
Thanks all for help, because It's very important for me !
Use this to interect with UI Thread for operations like updating textviews etc.
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
//YOUR CODE HERE
Message message = new Message();
message.obj = comingMsg;
mHandler2.sendMessage(message);
}
});
Hello friends i am creating mp3 player application my application is successfully build i want implement click event notification when user goes on background they control media from notification panel my notification also build successfully but problem is that how control music and change image on notification .addaction() when user click on pause the image change to play and when song is play image back change to pause and media player is also and i am also want get songs title, and artist here my code you can easily understand!
public void play(int songindex) {
song = songList.get(songindex);
try {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
Uri uri = Uri.parse("file:///" + song.getGetpath());
mediaPlayer = MediaPlayer.create(mContext, uri);
title.setText(song.getTitle());
artist.setText(song.getArtist());
notificationTitleText=title.getText();
notificationDescText=artist.getText();
handler = VisualizerDbmHandler.Factory.newVisualizerHandler(getApplicationContext(), mediaPlayer);
audioVisualization.linkTo(handler);
mediaPlayer.start();
seekBar.setProgress(0);
seekBar.setMax(100);
updateProgressBar();
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
play.setVisibility(View.GONE);
pause.setVisibility(View.VISIBLE);
play_main.setVisibility(View.GONE);
pause_main.setVisibility(View.VISIBLE);
Animation aniRotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
rotate.startAnimation(aniRotate);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (checked) {
mediaPlayer.setLooping(true);
mediaPlayer.start();
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songList.size() - 1) - 0 + 1) + 0;
play(currentSongIndex);
} else {
// no repeat or shuffle ON - play next song
if (currentSongIndex < (songList.size() - 1)) {
play(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
play(0);
currentSongIndex = 0;
}
}
}
});
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_SHORT).show();
}
}
public void shownotification(){
Bitmap largeImage = BitmapFactory.decodeResource(getResources(),R.drawable.dog);
Notification channel = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID_1)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle(notificationTitleText
)
.setContentText(notificationDescText)
.setLargeIcon(largeImage)
.addAction(R.drawable.ic_like,"like",null)
.addAction(R.drawable.ic_prev,"prev",null)
.addAction(R.drawable.ic_pause,"pause",null)
.addAction(R.drawable.ic_next,"next",null)
.addAction(R.drawable.ic_dislike,"dislike",null)
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().
setShowActionsInCompactView(1,2,3))
.build();
mNotificationManagerCompat.notify(1,channel);
}
gettext()method is working fine but it work on first when any clicked event is happen if song play oncomplete and next song is not get text value
I am assuming that you are playing songs from an Activity but this will also work for a service.
Put this in your activity or service
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.mypackage.ACTION_PAUSE_MUSIC")){
//Do whatever you want. Ex. Pause
}
//Similarly this can be done for all actions
}};
Make your show notification method like this
public void shownotification(){
Bitmap largeImage = BitmapFactory.decodeResource(getResources(),R.drawable.dog);
Intent pauseIntent = new Intent("com.mypackage.ACTION_PAUSE_MUSIC");
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 1, pauseIntent, 0);
// Similarly you can create an intent and pending intent pair for each action you want just change the string in intent constructor
Notification channel = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID_1)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle(notificationTitleText
)
.setContentText(notificationDescText)
.setLargeIcon(largeImage)
.addAction(R.drawable.ic_like,"like",null)
.addAction(R.drawable.ic_prev,"prev",null)
.addAction(R.drawable.ic_pause,"pause",pausePendingIntent) //like this attach every action with respective pending intent
.addAction(R.drawable.ic_next,"next",null)
.addAction(R.drawable.ic_dislike,"dislike",null)
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(1,2,3))
.build();
mNotificationManagerCompat.notify(1,channel);}
I want to add one more thing to #Kumar Manas answer
i.e We need to register reciever that is being created in activity.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.mypackage.ACTION_PAUSE_MUSIC")){
//Do whatever you want. Ex. Pause
}
//Similarly this can be done for all actions
}};
To register your Reciever add these lines in onCreate()
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.mypackage.ACTION_PAUSE_MUSIC");
registerReceiver(receiver,intentFilter);
Note: You can add as many actions you want
You may get your answer by following this tutorial "Android Music Player Controls on Lock Screen and Notifications"
Let me start by saying that if image shooting interval is anything more than 1 second it works. For example taking a picture every 2 seconds works perfectly fine. But taking a picture every second sometimes throws java.lang.RuntimeException: takePicture failed. What could be causing this kind of a behaviour?
Here is the code I use and it is in Service:
#Override
public void onCreate()
{
super.onCreate();
prefs = getSharedPreferences("general",Context.MODE_PRIVATE);
handler = new Handler();
shotInterval = prefs.getInt(getString(R.string.prefs_int_imageShootingFrequency),1);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
} else {
cameraId = findBackFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
cameraParameters = camera.getParameters();
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); //set camera to continuously auto-focus
camera.setParameters(cameraParameters);
pictureTaker.run(); // Start looping
}
Runnable pictureTaker = new Runnable() {
#Override
public void run() {
try {
takePicture();
} finally {
// 100% guarantee that this always happens, even if
// your update method throws an exception
handler.postDelayed(pictureTaker, shotInterval*1000);
}
}
};
private void takePicture(){
SurfaceView view = new SurfaceView(this);
try {
camera.setPreviewDisplay(view.getHolder());
camera.startPreview();
camera.takePicture(null, null,new PhotoHandler(getApplicationContext()));
} catch (IOException e) {
e.printStackTrace();
}
}
You should launch postDelayed() from the onPictureTaken() callback. You can check the system timer on call to takePicture() and reduce the delay respectively, to keep 1000ms repetition, but maybe once in a while, this delay will reach 0.
my android application freezes for a few seconds when i try to send my database file to my remote server, is there anyway i can set this as a background thread using multithreading or another such feature?
this is the code for the dialog box that appears to ask me if i want to send
public void showYesNoBox(){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
SendData();
finish();//go back to the previous Activity
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
finish();//go back to the previous Activity
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK);
builder.setMessage("Do you want to send the data now?");
builder.setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener);
builder.show();
}
and here is the code it executes when i click the "Yes" button
public void SendData() {
File path = getDatabasePath(DataBaseHelper.DATABASE_NAME);
if (path.exists())
copyDatabase(path);
}
/**
* Copy the database to the sdcard
* #param file
*/
private void copyDatabase(File file) {
SimpleDateFormat dateFormat1 = new SimpleDateFormat("ddMMyy-HHmmss");
String dateString = dateFormat1.format(new Date());
String pathdest = getDir().getAbsolutePath() + Configuration.LOST_FOLDER + "/Database/";
String pathdir = pathdest;
File dir = new File(pathdir);
if (!dir.exists())
dir.mkdirs();
String namefile = file.getName();
int pos = namefile.lastIndexOf('.');
if (pos != -1) {
String ext = namefile.substring(pos + 1);
String name = namefile.substring(0, pos - 1);
pathdest += name;
pathdest += "_" + dateString;
pathdest += ext;
} else {
pathdest += namefile;
pathdest += "_" + dateString;
}
File filedest = new File(pathdest);
try {
if (filedest.createNewFile())
copyFile(file.getAbsolutePath(), pathdest);
} catch (IOException e) {
e.printStackTrace();
}
}
any help with this problem would be greatly appreciated as while the problem doesn't prevent the user from being able to send the data it is anoying to be stuck on one screen for a few seconds while it attempts to execute the action.
while the problem doesn't prevent the user from being able to send the data it is anoying to be stuck on one screen for a few seconds while it attempts to execute the action.
You can see AsyncTask. Its for the same purposes. User need not be stuck on UI when you want to do long running processes including taking to server, doing database stuff etc. This can be done in the background using AsyncTask. There are many good tutorials out there. Just Google it. Checkout Android Background Processing with Threads, Handlers and AsyncTask - Tutorial and What arguments are passed into AsyncTask<arg1, arg2, arg3>? . Hope this helps.
I've been using asynctask for handling all the processes I want to do in the background.
AsyncTask
Use all network operations in AsyncTask
like the below example
private class UploadTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
// upload task
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Once created, a task is executed very simply:
new UploadTask().execute(url1, url2, url3);