Updating Activity TextView from Service - android

I need to download 100MB of images, so i decided that the best way is to make Service wich download it, and will show results for each file in activity. But this works like theres no service, the Activity fzreees, and unfreezes only after download all files.
Heres the code of Activity:
public class DownloadActivity extends Activity
{
String hist;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.download_activity);
startService(new Intent(this, DownloadService.class));
registerReceiver(broadcastReceiver,
new IntentFilter(DownloadService.BROADCAST_ACTION));
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent _intent)
{
updateUI(_intent);
}
};
private void updateUI(Intent intent)
{
if (intent.getBooleanExtra("exists", false))
hist = hist + "Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("obraz", -1) + " - DOWNLOADED\n";
else
hist = hist + "Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("obraz", -1) + " - ALREADY EXISTS\n";
((TextView) findViewById(R.id.dtitle)).setText("Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("image", -1) + ".");
((TextView) findViewById(R.id.ddetails)).setText(hist);
}
}
Code of Service:
public class DownloadService extends Service
{
public static final String BROADCAST_ACTION = "emis.katalog.grzybow.publishprogress";
Intent intent;
int counter = 0;
String postString;
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public void onStart(Intent intent, int startId)
{
SQLiteDatabase db = new BazaGrzybowHelper(DownloadService.this).getReadableDatabase();
Cursor kursor = db.rawQuery("SELECT * FROM table", null);
InputStream in = null;
OutputStream out = null;
URL ulrn;
int nn = 1;
int pos = 1;
//out:
while(kursor.moveToNext())
{
while(kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != "" ||
kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != null)
{
String filename = "thg_" + pos + "_" + (nn+2) + ".jpg";
if (new File(Environment.getExternalStorageDirectory(),
"emis/katalog.grzybow/zapis_na_stale/"+filename).exists())
publishProgress(pos, nn, true);
else
{
publishProgress(pos, nn, false);
File destDir = new File(Environment.getExternalStorageDirectory(),
"emis/katalog.grzybow/zapis_na_stale");
if (!destDir.exists())
destDir.mkdirs();
destDir = null;
try
{
ulrn = new URL(kursor.getString(kursor.getColumnIndex("i_url_" + nn)));
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
in = con.getInputStream();
out = new FileOutputStream(Environment.getExternalStorageDirectory().
getPath() + "/emis/katalog.grzybow/zapis_na_stale/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
nn++;
if (nn > 10 || kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == "" ||
kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == null)
{
nn = 1;
break;
}
/*if (anuluj)
break out;*/
}
pos++;
}
db.close();
}
private void publishProgress(int item, int image, boolean exists)
{
intent.putExtra("item", item);
intent.putExtra("image", image);
intent.putExtra("exists", exists);
sendBroadcast(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
}
What am I doing wrong?

Perhaps this?
Caution: A service runs in the main thread of its hosting process—the
service does not create its own thread and does not run in a separate
process (unless you specify otherwise). This means that, if your
service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread
within the service to do that work. By using a separate thread, you
will reduce the risk of Application Not Responding (ANR) errors and
the application's main thread can remain dedicated to user interaction
with your activities.

Related

how to solve service on a null object reference [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have service class to download some data ,(before i am using Asynctask to download data but now i need to use service).
when i initializ service class so i am getting this erroe
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.startService(ContextWrapper.java:606)
at com.monnfamily.libraryapp.Utility.DownloadManager.downloadBookData(DownloadManager.java:55)
at com.monnfamily.libraryapp.Contentful.ContentfulAdapter$1$2.onClick(ContentfulAdapter.java:99)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:173)
So Please give me any suggestion to overcome this error
public class Services extends Service {
private DownloadManager.DownloadCompletedListner mListner;
private BookProperties mBookDetails;
private Context context;
public Services(DownloadManager.DownloadCompletedListner mListner, BookProperties mBookDetails) {
// super("Downloader");
this.mListner = mListner;
this.mBookDetails = mBookDetails;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
Log.i("onStartCommand","service sudddddd");
String tMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("MonnFamily", Context.MODE_PRIVATE));
tMainFolder += "/Book" + mBookDetails.getBookId();
Log.i(TAG, "book id and book name" + mBookDetails.getBookId() + mBookDetails.getBookName());
downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_IMAGE + ".png", mBookDetails.getBookMainImage());
downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_AUDIO + ".mp3", mBookDetails.getBookSound());
for (PageDetailProperties pageDetails : mBookDetails.getPageDetail()) {
String tPageNumber = pageDetails.getPageNumber().toString();
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_IMAGE + tPageNumber + ".png", pageDetails.getPageImage());
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_AUDIO + tPageNumber + ".mp3", pageDetails.getPageAudio());
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_TEXT + tPageNumber + ".txt", pageDetails.getPageText());
}
// myddownloadmethod();// calling my download method
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.d("Error",e1.getMessage());
}
return super.onStartCommand(intent, flags, startId);
}
private void downloadBookDetails(String pMainFolder, String pFileName, String pDownloadURL) {
Log.i(TAG, "Coming to this downloadBookDetails ");
try {
URL url = new URL(pDownloadURL);
URLConnection ucon = url.openConnection();
Log.i("Service" , "run in " + url);
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
File directory = new File(pMainFolder, pFileName);
FileOutputStream outStream = new FileOutputStream(directory);
byte[] buff = new byte[5 * 1024];
int len;
while ((len = inStream.read(buff)) != -1) {
outStream.write(buff, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
} catch (Exception e) {
//Add Network Error.
Log.e(TAG, "Download Error Exception " + e.getMessage());
e.printStackTrace();
}
}
public void downloadBookData(BookProperties pBook){
mBookDetails = pBook;
String tMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("MonnFamily", Context.MODE_PRIVATE));
tMainFolder += "/Book" + pBook.getBookId();
File directory = new File(tMainFolder);
if (!directory.exists()) {
directory.mkdir();
((LibraryView)BaseApplication.getInstance().getCurrentActivity()).showActivityView();
// new MyDownloaderAsyncTask(mListner,mBookDetails).execute();
new Services(mListner,mBookDetails).getApplicationContext().startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class)); // My service call
You are getting context from unfired service at this line that's the problem from
new Services(mListner,mBookDetails).getApplicationContext().startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class)); // My service call
if you are calling it at an Activity change it to :
startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class));
or from Fragment
getActivity().startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class));
else I thing BaseApplication.getInstance().getAppContext() return a Context so it can be used like this :
BaseApplication.getInstance().getAppContext().startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class));
Did You try to pass this instead of Context? I think it will solve your problem.
new Services(mListner,mBookDetails).getApplicationContext().startService(new Intent(BaseApplication.getInstance().getAppContext(),Services.class));
with
new Services(mListner,mBookDetails).ClassEx.this.startService(new Intent(ClassEx.this,Services.class));

How to make an app to copy text and send automatically to my networked database?

I am try to make Android app using clipboard manager
Using this code-
final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.addPrimaryClipChangedListener( new ClipboardManager.OnPrimaryClipChangedListener() {
public void onPrimaryClipChanged() {
String a = clipboard.getText().toString();
Toast.makeText(getBaseContext(),"Copy:\n"+a,Toast.LENGTH_LONG).show();
}
});
My app aim to copy text and send automatically my server database. Any ideas on creating this?
Firstly create a service which will listen even when the application is closed
public class CBWatcherService extends Service {
private final String tag = "[[ClipboardWatcherService]] ";
private OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() {
public void onPrimaryClipChanged() {
performClipboardCheck();
}
};
#Override
public void onCreate() {
((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).addPrimaryClipChangedListener(listener);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
File folder = new File(ClipboardCacheFolderPath);
// ClipboardCacheFolderPath is a predefined constant with the path
// where the clipboard contents will be written
if (!folder.exists()) { folder.mkdir(); }
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void performClipboardCheck() {
ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (cb.hasPrimaryClip()) {
ClipData cd = cb.getPrimaryClip();
if (cd.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
try {
File folder = new File(ClipboardCacheFolderPath);
if (!folder.exists()) { folder.mkdir(); }
Calendar cal = Calendar.getInstance();
String newCachedClip =
cal.get(Calendar.YEAR) + "-" +
cal.get(Calendar.MONTH) + "-" +
cal.get(Calendar.DAY_OF_MONTH) + "-" +
cal.get(Calendar.HOUR_OF_DAY) + "-" +
cal.get(Calendar.MINUTE) + "-" +
cal.get(Calendar.SECOND);
// The name of the file acts as the timestamp (ingenious, uh?)
File file = new File(ClipboardCacheFolderPath + newCachedClip);
file.createNewFile();
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
bWriter.write((cd.getItemAt(0).getText()).toString());
bWriter.close();
// Here send the file or the text you want to send to the server or firebase
//callApi()
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

IntentService blocking UI

I have one service(BackgoundService), one intent service (MyIntentService) and one process in my android app. UI hangs i.e. freezes completely when all three are running but it becomes so responsive when I stop the Intent Service.
Also I am running the BackgroundService in a worker thread and documentation says that Intent Service runs in a separate worker thread by default.
I don't get why my ui thread is still getting blocked when all other operations ,services are running in separate threads.
Intent service
public class MyIntentService extends IntentService {
public static String ACTION ="com.example.mediaapp.prototypingcanvas.MyIntentService.DONE_Processing";
public MyIntentService(){
super("MyIntentService");
}
DBRepo dbRepo;
WebRepo webRepo;
FileRepo fileRepo;
#Override
protected void onHandleIntent(Intent intent) {
//Activity Types ; 1=uploadSurvey; 2=downloadQre; 3=download survey for review; 4=sample download; 5=projectlistdownload
webRepo = new WebRepo(this);
fileRepo = new FileRepo();
int queueID = intent.getIntExtra("ID", 0);
dbRepo = new DBRepo(getApplicationContext());
dbRepo.updateQueue(queueID);
List<String> filesList;
int activityType = intent.getIntExtra("ActivityType", 0);
// int projectID = intent.getIntExtra("ProjectID",0);
String userName = intent.getStringExtra("UserName");
String ProjectGUID = intent.getStringExtra("ProjectGUID");
String projectName = intent.getStringExtra("ProjectName");
int response = -1;
Log.i("activitytype", activityType + "");
if (activityType == 1) {
filesList = new ArrayList<>();
filesList = fileRepo.list(projectName);
for (String fileName : filesList) {
webRepo.SyncData("http://test.cloud.rebuscode.com/api/api/v2/respondent/SyncData/" + projectName + "/" + fileName + "/true");
}
Log.i("activity type", activityType + "");
response = 1;
} else if (activityType == 2) {
Log.i("activity type", activityType + "");
response = 0;
} else if (activityType == 3) {
Log.i("activity type", activityType + "");
//webRepo.DownloadQuestionnaire("http://test.cloud.rebuscode.com/api/api/v2/respondent/DownloadQuestionnaireForTab/" + projectGuid + "/" + projectName,projectName);
fileRepo.unzip("Splash/and.zip");
} else if (activityType == 4) {
Log.i("activity type", activityType + "");
response = 1;
} else if (activityType == 5) {
Log.i("activity type", activityType + "");
response = 0;
} else {
Log.i("activity type", activityType + "");
}
// processing done here….
//delete row from Queue table and broadcast
dbRepo.deleteQueue(queueID);
Intent broadcastIntent = new Intent(ACTION);
broadcastIntent.putExtra("Response", response);
broadcastIntent.putExtra("ActivityType", activityType);
broadcastIntent.putExtra("UserName", userName);
broadcastIntent.putExtra("ID", queueID);
broadcastIntent.putExtra("ProjectGuid", ProjectGUID);
sendBroadcast(broadcastIntent);
}
}
can anyone suggest why its blocking my ui thread???

Android - Passing long array via broadcast intent

I wrote a little android program, there is a main activity with a broadcast listener, and i create another thread. The thread searches for prime numbers, and loading them into a long arraylist, and after every 3 seconds, sends the filled array to the main activity via broadcast. Everythings ok, until i'm trying to get the long array extra from the intent. It causes every time a nullpointerexception.
I tried with a string arraylist, it worked, but i am curious because the intent has an "getlongarrayextra" method.
Here is my code:
public class MainActivity extends Activity {
public static String BROADCAST_THREAD_KEY = "broadcast_key";
public static String EXTRAARRAYID = "primes";
private static long MAXNUM = 2000000;
private PrimeCalculatorThread thread;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.numberstext);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(android.content.Context context,
android.content.Intent intent) {
String origitext = textView.getText().toString();
long[] primes = intent.getExtras().getLongArray(EXTRAARRAYID);
Log.d("ASD", "broadcast received" + primes.toString());
StringBuilder builder = new StringBuilder();
if (primes != null) {
for (long prime : primes) {
builder.append(prime + " - ");
}
textView.setText(origitext + "\n" + builder.toString());
}
};
};
#Override
protected void onResume() {
Log.d("ASD", "ONRESUME");
initReceiverAndStartThread();
super.onResume();
}
private void initReceiverAndStartThread() {
IntentFilter filter = new IntentFilter(BROADCAST_THREAD_KEY);
registerReceiver(receiver, filter);
thread = new PrimeCalculatorThread(getBaseContext(), MAXNUM);
thread.start();
Log.d("ASD", "THREAD STARTED");
}
and the second thread:
public class PrimeCalculatorThread extends Thread {
private Context context;
private long maxnum;
List<Long> primes;
boolean isrunning;
public void setIsrunning(boolean isrunning) {
this.isrunning = isrunning;
}
private long counter = 0;
private long DELAYBETWEENBROADCAST = 3000;
public PrimeCalculatorThread(Context c, long maxnum) {
this.context = c;
this.maxnum = maxnum;
primes = new ArrayList<Long>();
}
#Override
public void run() {
long startTime = System.currentTimeMillis();
long estimatedTime;
isrunning = true;
for (long i = 0; i < maxnum; i++) {
Log.d("ASD", Boolean.toString(isrunning));
if (!isrunning)
break;
Log.d("ASD", i + "");
estimatedTime = System.currentTimeMillis() - startTime;
if (isPrime(i)) {
primes.add(i);
Log.d("ASD", i + "is a prime");
} else {
Log.d("ASD", i + "is not a prime");
}
if (estimatedTime > counter * DELAYBETWEENBROADCAST
+ DELAYBETWEENBROADCAST) { // elapsed another period
Log.d("ASD", primes.toString() + " will be sending.");
sendBroadCast();
primes.clear();
counter++;
}
try { //for debug purposes
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void sendBroadCast() {
Intent intent = new Intent(MainActivity.BROADCAST_THREAD_KEY);
intent.putExtra(MainActivity.EXTRAARRAYID, primes.toArray());
context.sendBroadcast(intent);
Log.d("ASD", "BROADCAST SENT" + primes.toString());
}
boolean isPrime(long n) {
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return false;
}
return true;
}
}
The problem is that you are managing a list of Long objects and passing it in putExtra, which means you are invoking putExtra(String name, Serializable value). Then you try to get that value using getLongArray(), but you haven't put any long array extra, you see! To solve this, replace
intent.putExtra(MainActivity.EXTRAARRAYID, primes.toArray());
with
long[] primesArray = new long[primes.size()];
for (int i = 0; i < primes.size(); i++) {
primesArray[i] = primes.get(i);
}
intent.putExtra(MainActivity.EXTRAARRAYID, primesArray);
This will invoke the correct putExtra(String name, long[] value) method.

FTP file upload issue

I need to upload a file from the sdcard to a server(using FTP), but I've encountered some problems when trying to save the file. The file seems to exist at first, but when I choose to play it... well, it just won't (like it crashes during upload rendering it invalid). Any ideas regarding what might going wrong? I've set the extension .mp3, and here's the code:
holder.upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str_useid = Recording.str_getValue;
if (upload_or_not == true) {
FTPClient con = new FTPClient();
try {
con.connect("FTP URL");
if (con.login("USERNAME", "PASSWORD")) {
Handler progressHandler = new Handler();
con.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = new BufferedInputStream(
new FileInputStream(
"/sdcard/audiometer/shanesh" +
RequestId[position] + "-"
str_useid + ".mp3"
)
);
con.enterLocalPassiveMode(); // important!
ProgressInputStream progressInput = new ProgressInputStream(
buffIn,
progressHandler
);
boolean status = con.storeFile(
"/Rangam/shanesh" +
RequestId[position] + "-" +
str_useid +
".mp3",
progressInput
);
String filename = "/shanesh" + RequestId[position] +
"-" + str_useid + ".mp3";
buffIn.close();
con.logout();
con.disconnect();
String MachineName = "DOT-NET-SERVER";
sendFlagToServer(RequestId[position], filename, MachineName);
Toast.makeText(
context,
" :: sucessfully upload :: " + status,
Toast.LENGTH_LONG
).show();
Toast.makeText(
context,
" :: RequestId is :: " +
RequestId[position],
Toast.LENGTH_LONG
).show();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(
context,
" :: File not Found :: ",
Toast.LENGTH_LONG
).show();
}
}
ProgressInputStream.java
package com.RecordingApp;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class ProgressInputStream extends InputStream {
/* Key to retrieve progress value from message bundle passed to handler */
public static final String PROGRESS_UPDATE = "progress_update";
private static final int TEN_KILOBYTES = 1024 * 10;
private InputStream inputStream;
private Handler handler;
private long progress;
private long lastUpdate;
private boolean closed;
public ProgressInputStream(InputStream inputStream, Handler handler) {
this.inputStream = inputStream;
this.handler = handler;
this.progress = 0;
this.lastUpdate = 0;
this.closed = false;
}
#Override
public int read() throws IOException {
int count = inputStream.read();
return incrementCounterAndUpdateDisplay(count);
}
#Override
public int read(byte[] b, int off, int len) throws IOException {
int count = inputStream.read(b, off, len);
return incrementCounterAndUpdateDisplay(count);
}
#Override
public void close() throws IOException {
super.close();
if (closed)
throw new IOException("already closed");
closed = true;
}
private int incrementCounterAndUpdateDisplay(int count) {
if (count > 0)
progress += count;
lastUpdate = maybeUpdateDisplay(progress, lastUpdate);
return count;
}
private long maybeUpdateDisplay(long progress, long lastUpdate) {
if (progress - lastUpdate > TEN_KILOBYTES) {
lastUpdate = progress;
sendLong(PROGRESS_UPDATE, progress);
}
return lastUpdate;
}
public void sendLong(String key, long value) {
Bundle data = new Bundle();
data.putLong(key, value);
Message message = Message.obtain();
message.setData(data);
handler.sendMessage(message);
}
}

Categories

Resources