class WorkingThread extends Thread{ @Override public void run() { // } }
class WorkingClass implements Runnable{ @Override public void run() { // } } WorkingClass workingClass = new WorkingClass(); Thread thread = new Thread(workingClass); thread.start();
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); WorkingClass workingClass = new WorkingClass(); Thread thread = new Thread(workingClass); thread.start(); } class WorkingClass implements Runnable{ @Override public void run() { // // UI Runnable textView.post(new Runnable() { @Override public void run() { textView.setText("The job is done!"); } }); } } }
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); WorkingClass workingClass = new WorkingClass(); Thread thread = new Thread(workingClass); thread.start(); } class WorkingClass implements Runnable{ @Override public void run() { // // UI Runnable MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { textView.setText("The job is done!"); } }); } } }
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); WorkingClass workingClass = new WorkingClass(true); Thread thread = new Thread(workingClass); thread.start(); } class WorkingClass implements Runnable{ public static final int SUCCESS = 1; public static final int FAIL = 2; private boolean dummyResult; public WorkingClass(boolean dummyResult){ this.dummyResult = dummyResult; } @Override public void run() { // // if (dummyResult){ // uiHandler.sendEmptyMessage(SUCCESS); } else { // Message msg = Message.obtain(); msg.what = FAIL; msg.obj = "An error occurred"; uiHandler.sendMessage(msg); } } } Handler uiHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { switch (msg.what) { case WorkingClass.SUCCESS: textView.setText("Success"); return true; case WorkingClass.FAIL: textView.setText((String)msg.obj); return true; } return false; } }); }
public class MainActivity extends Activity { private static final String IMAGE_URL = "http://eastbancgroup.com/images/ebtLogo.gif"; TextView textView; ImageView imageView; ProgressDialog progressDialog; DownloadTask downloadTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); imageView = (ImageView)findViewById(R.id.imageView); downloadTask = new DownloadTask(); // , downloadTask.execute(IMAGE_URL); } @Override protected void onStop() { // , // Activity downloadTask.cancel(true); super.onStop(); } /* * AsyncTask<Params, Progress, Result> * , generic-. * Params - . String, .. * url * Progress - , . * Integer. * Result - . Drawable. */ class DownloadTask extends AsyncTask<String, Integer, Drawable>{ @Override protected void onPreExecute() { // progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setIndeterminate(false); progressDialog.setMax(100); progressDialog.setProgress(0); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("Downloading Image"); progressDialog.show(); } @Override protected Drawable doInBackground(String... params) { // // URLConnection int count; try { URL url = new URL(params[0]); URLConnection conection = url.openConnection(); conection.connect(); int lenghtOfFile = conection.getContentLength(); InputStream input = new BufferedInputStream(url.openStream(), 8192); OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg"); byte data[] = new byte[256]; long total = 0; while ((count = input.read(data)) != -1) { //, if (isCancelled()){ return null; } total += count; output.write(data, 0, count); // . // , // //onProgressUpdate publishProgress((int)((total*100)/lenghtOfFile)); } output.flush(); output.close(); input.close(); } catch (Exception e) { Log.e("Error: ", e.getMessage()); } String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; return Drawable.createFromPath(imagePath); } @Override protected void onProgressUpdate(Integer... progress) { progressDialog.setProgress(progress[0]); } // @Override protected void onPostExecute(Drawable result) { imageView.setImageDrawable(result); progressDialog.dismiss(); } // onPostExecute, // //AsyncTask#cancel(boolean mayInterruptIfRunning) @Override protected void onCancelled() { } } }
public class MainActivity extends Activity { TextView textView; private int counter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); counter = 0; textView = (TextView)findViewById(R.id.hello); // new Thread(new WorkingClass()).start(); } class WorkingClass implements Runnable{ public static final int RELAUNCH = 1; private static final int DELAY = 1000; @Override public void run() { // // 1000ms uiHandler.sendEmptyMessageDelayed(RELAUNCH, DELAY); } } Handler uiHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { // if (msg.what == WorkingClass.RELAUNCH){ textView.setText("Times: "+counter); counter++; new Thread(new WorkingClass()).start(); return true; } return false; } }); }
public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor> { // static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS, }; private static final int LOADER_ID = 1; private SimpleCursorAdapter adapter; TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //, textview = (TextView)findViewById(R.id.loading); // , getListView().setVisibility(View.GONE); // ListView adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, null, new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, new int[] { android.R.id.text1, android.R.id.text2 }, 0); setListAdapter(adapter); // Loader' // id Loader' callback LoaderManager lm = getLoaderManager(); lm.initLoader(LOADER_ID, null, this); } // Loader, // @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { Uri baseUri = Contacts.CONTENT_URI; String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; return new CursorLoader(this, baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); } // , // , @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { switch (loader.getId()) { case LOADER_ID: adapter.swapCursor(cursor); textview.setVisibility(View.GONE); getListView().setVisibility(View.VISIBLE); break; } } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } }
public class BackgroundService extends Service { public static final String CHANNEL = BackgroundService.class.getSimpleName()+".broadcast"; // , // Intent @Override public int onStartCommand(Intent intent, int flags, int startId) { // sendResult(); return Service.START_NOT_STICKY; } // , // Broadcast private void sendResult() { Intent intent = new Intent(CHANNEL); sendBroadcast(intent); } @Override public IBinder onBind(Intent intent) { return null; } }
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); // registerReceiver(receiver, new IntentFilter(BackgroundService.CHANNEL)); // , Intent Intent intent = new Intent(this, BackgroundService.class); startService(intent); } @Override protected void onStop() { // unregisterReceiver(receiver); super.onStop(); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { textView.setText("Message from Service"); } }; }
public class DownloadService extends IntentService { public DownloadService() { super("DownloadService"); } public static final String CHANNEL = DownloadService.class.getSimpleName()+".broadcast"; private void sendResult() { Intent intent = new Intent(CHANNEL); sendBroadcast(intent); } @Override public IBinder onBind(Intent intent) { return null; } // @Override protected void onHandleIntent(Intent intent) { // // sendResult(); } }
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.hello); // registerReceiver(receiver, new IntentFilter(DownloadService.CHANNEL)); // , Intent Intent intent = new Intent(this, DownloadService.class); startService(intent); } @Override protected void onStop() { // unregisterReceiver(receiver); super.onStop(); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { textView.setText("Message from Service"); } }; }
public class MainActivity extends Activity { private static final String IMAGE_URL = "http://eastbancgroup.com/images/ebtLogo.gif"; ImageView imageView; DownloadManager downloadManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)findViewById(R.id.imageView); // DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // Request request = new Request(Uri.parse(IMAGE_URL)); request.setTitle("Title"); // request.setDescription("My description"); // request.setMimeType("application/my-mime"); //mine type // , , //- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // downloadManager.enqueue(request); } @Override protected void onResume() { super.onResume(); // registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED)); }; @Override protected void onPause() { super.onPause(); // unregisterReceiver(receiver); }; BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // , if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){ long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor cursor = dm.query(query); if (cursor.moveToFirst()){ int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) { String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); imageView.setImageURI(Uri.parse(uriString)); } } // } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)){ DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); // , // , // long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(ids); Cursor cursor = dm.query(query); int idIndex = cursor.getColumnIndex(DownloadManager.COLUMN_ID); if (cursor.moveToFirst()){ do { // id // long downloadId = cursor.getLong(idIndex); } while (cursor.moveToNext()); } } } }; }
<intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:mimeType="application/my-mime" /> (mime type, ) <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
Source: https://habr.com/ru/post/192998/
All Articles