android-context

How to Stop Timer Android

亡梦爱人 提交于 2019-12-08 20:46:30
following is my code: In MainActivity.class private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) { if(CheckedButtonId==R.id.radiobutton_on){ Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show(); alert_on = true; display_alert(); } else{ alert_on = false; } } }; public void display_alert(){ int delay = 10000; Timer timer =new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);

Can't make static reference to non-static method ( Android getApplicationContext() )

跟風遠走 提交于 2019-12-08 18:03:05
问题 I have been storing a global variable that's needed across Activities in my Android app by using a subclass of android.app.Application as explained by Soonil (in How to declare global variables in Android?). The approach looks like this: class MyApp extends Application { private String myState; public String getState(){ return myState; } public void setState(String s){ myState = s; } } class Blah extends Activity { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp

Rename a file in the internal storage

南楼画角 提交于 2019-12-08 16:33:27
问题 What's the best/easiest way to rename a file in the application's internal storage? I find it a bit strange that there is a Context.deleteFile() method, but no "move" or "rename" function. Do I have to go all the way through saving the file's contents, deleting it, creating a new one and then copying the contents into that one? Or is there a way to copy a file over an existing file? Update (Aug. 30, 2012): As per the suggested solution below, which I cannot get to work: I have a file called

How to pass context from activity to activity?

南笙酒味 提交于 2019-12-08 16:27:42
问题 I have a main activity, and it summons another activity to display some data. I have a private database helper object that I use throughout the main activity code. Is there a way to pass the context of my main activity to my sub activity in an elegant way? (ie, from subclass, something like getCallingActivityContext() ) I could always create new database helper objects. 回答1: Extending the Application class helps you to allow declare/access global variables. You can set your variables from any

Please explain me Context class in Android

岁酱吖の 提交于 2019-12-08 16:23:05
问题 I'm new to Android. Can someone explain me the concept of Context class/Object. What it is? What it will be used for? Why Context class? 回答1: Have you seen the android developer's guide? it will answer your questions: Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as

Toast: Difference between “this” and “getApplicationContext()”?

假如想象 提交于 2019-12-08 16:03:24
问题 My device runs Android 5.1.1 and I found out that if I use Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show(); I got this: But if I use getApplicationContext() instead of this , Toast.makeText(getApplicationContext(), "This is a toast", Toast.LENGTH_SHORT).show(); I got this: Both are called directly from the activity. Why is this? 回答1: It has to do with the Theme the Context has associated with it. Using this is using a context (I'm assuming your Activity or Fragment ) that

How to use Context to access/manipulate another class/activity

老子叫甜甜 提交于 2019-12-08 09:17:30
I want to create a generic AsynceTask class that all my activities use/share for downloading content from a url. for this reason, I don't want the OnPostExecute to do anything other than to send the content back to some method in the activity that invoked the AsyncTask class. I know that I need to create a constructor that sets the context of the Activity that invoked the AsyncTask , but then what, how do I use the context to send something back the the activity corresponding to that context. I've seen no tutorials that show how to use context in this manner. Lets say I have: public class

Fragment onStop() versus onDetach(), onDestroy() or onDestroyView() of the same?

你。 提交于 2019-12-08 09:16:22
问题 This is the question worth to be asked on the behalf of the new developers in Android. Idea is to provide a deep understanding to why the framework is written as such. Also, developers face dangling pointers, illegal states and such run-time crashes, and they do not have a clue that why it is happening. Programmers now a days heavily use call back and factory patterns. Use of delegate class objects reduce the need for Singleton classes, and the need of multiple inheritance in languages like C

How to Stop Timer Android

喜夏-厌秋 提交于 2019-12-08 08:13:16
问题 following is my code: In MainActivity.class private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) { if(CheckedButtonId==R.id.radiobutton_on){ Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show(); alert_on = true; display_alert(); } else{ alert_on = false; } } }; public void display_alert(){ int delay = 10000; Timer timer =new Timer(); timer.scheduleAtFixedRate(new

How to use Context to access/manipulate another class/activity

僤鯓⒐⒋嵵緔 提交于 2019-12-08 06:18:54
问题 I want to create a generic AsynceTask class that all my activities use/share for downloading content from a url. for this reason, I don't want the OnPostExecute to do anything other than to send the content back to some method in the activity that invoked the AsyncTask class. I know that I need to create a constructor that sets the context of the Activity that invoked the AsyncTask , but then what, how do I use the context to send something back the the activity corresponding to that context.