Create notification from non-activity class - android

Is it possible to create notification from non-activity class? If so, how?

pass the context in to the class and then create it normally

class A extends Activity{
//required stuff go here
new B().createDialog(A.this);
}
other class
class B{
public void createDialog(Context context){
//create your dialog or notification here
}
}

As subspider said above, pass the context into the class and you'll be fine:
public class DoSomethingClass {
//Here's a context
private Context _CONTEXT;
//Construct that sets the context
public DoSomethingClass(Context c) {
this._CONTEXT = c;
}
public void createNotification() {
/*
Create the notification as usual, just make sure you alter the following lines:
Intent notificationIntent = new Intent(this, MyClass.class);
Context context = this.getApplicationContext();
^Make sure you alter this into this._CONTEXT above
*/
}
}

Related

How to initialize context where we cannot get context from getApplicationContext()?

I want to access a class extending SQLiteOpenHelper to get the context of database from a java class. I need to pass application context to get that but don`t have access to getApplicationContext().
How can I get Application Context in java class that is not activity?
I suggest you create a constructor that has a parameter of the Context type.
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
// Variables
private Context ctx;
public MySQLiteOpenHelper(Context ctx) {
this.ctx = ctx;
}
//More code
}
Now, in your activities, you can do this:
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
And in your fragments, you can do this:
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(getActivity().getApplicationContext()); //getActivity() would work too, Activity (indirectly) extends Context.
You can create custom Application class and write method getContext().
Someting like this:
public class MyApplication extends Application {
private static MyApplication mCurrentInstance;
#Override
public void onCreate() {
super.onCreate();
mCurrentInstance = this;
}
public static MyApplication instance() {
return mCurrentInstance;
}
public static Context context() {
return mCurrentInstance.getApplicationContext();
}
}
And add this class into manifest:
<application
android:name=".MyApplication">

Class throwing Null Pointer Exception

Why does the following method throws an NPE,
public ActivityOne extends Activity{
DataManager dtMan = new DataManager(this)
public onCreate (){
...some source code here...
dtMan.check();
}
}
public class DataManager(){
private Context myContext;
public DataManager (Context context){
myContext = context;
}
Helper helper = new Helper(this);
public boolean check(){
helper.open();
...some source code here...
}
}
When I view the logcat; I get a java.null.pointer exception, so I did something like
public class DataManager(){
private Context myContext;
public DataManager (Context context){
myContext = context;
}
public boolean check(Context context){
**Helper helper = new Helper(context);**
helper.open();
...some source code here...
}
}
And it worked, so what is the difference between the two DataManager in Java/Android programming perspective, thus this approach if I understand correctly must be replicated to as follow:
public class DataManager(){
private Context myContext;
public DataManager (Context context){
myContext = context;
}
public boolean check(Context context){
**Helper helper = new Helper(context);**
helper.open();
...some source code here...
}
public boolean check2(Context context){
**Helper helper = new Helper(context);**
helper.open();
...some source code here...
}
public boolean check3(Context context){
**Helper helper = new Helper(context);**
helper.open();
...some source code here...
}
}
Meaning I just can't declare the Helper Class once and use it anywhere the calling class, did I forgot some fundamentals? Please clarify.
Will the context also lead to memory leaks?, if so, how will I fix it?
#EDIT:
Well I forgot to include the Helper Class
public class Helper{
private Context myContext;
public Helper(Context context){
myContext = context;
}
public void open(){
//do stuff here
}
}
Your code does not work because the DataManager is initialized at field scope in the Activity.
Classes which need a Context should always be initialized in one of the Activity's life cycle methods:
onCreate, onStart, onDestroy, etc.
Like this:
private DataManager dataManager;
public void on create(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dataManager = new DataManager(this):
}
You also should initialize the Helper class in the DataManager constructor:
public class DataManager(){
private Context context;
private Helper helper;
public DataManager (Context context){
this.context = context;
this.helper = new Helper(this.context);
}
public boolean check(Context context){
helper.open();
//...some source code here...
}
}
To prevent memory leaks you just need to make sure the DataManager class is not a static instance with an Activity Context.
If you don't need an Activity Context but you're also fine with an Application Context you should use it:
public DataManager(Context context){
this.context = context.getApplicationContext();
//...
}
Try put
dtMan = new DataManager(this)
in onCreate method in Activity?

Starting an Android Activity from a static method

I want to start an activity from a static java method on an android device.
I do not have any context or anything passed as parameter to the static function.
For starting the activity I must call "startActivity" with the current running method as "this" pointer. So is there a way to get the current running activity?
You can access only static variables/objects inside static method.
So You need to Implement this way
public class MainActivity extends Activity {
private static Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
}
public static void goToLoginActivity() {
Intent login = new Intent(mContext, LoginActivity.class);
mContext.startActivity(login);
}
}
NOTE : But this is not the proper way to do so, this may cause window leak issue.
Better approach is pass activity/context object as parameter like this.
public static void goToLoginActivity(Context mContext) {
Intent login = new Intent(mContext, LoginActivity.class);
mContext.startActivity(login);
}
Create a Class in your app extending class Application, define a static context and initialise this with your application context. You can expose a static method from this class for accessing defined static reference. Thats it.
class MyApp extends Application{
private static Context mContext;
public void onCreate(){
mContext = this.getApplicationContext();
}
public static Context getAppContext(){
return mContext;
}
}
Now you can use this static method for accessing context anywhere in your app.

Android. How to Get context from different activity

I'm playing with the GCM.
Everything is perfect using the example on https://code.google.com/p/gcm/source/checkout
Im getting notifications on my app with the gcm messages,
Now I want to add the message in a listView located on my MainActivity.
Im receiving my messages on a different class (GcmIntentService.java). How can I get MainActivity context to sendBroadcast.
Already tried with
private static Context mContext;
public static Context getContext() {
return mContext;
}
public static void setContext(Context context) {
mContext = context;
}
But is not working.
Any Ideas.
Thanks
I am not sure what you are doing. But keeping the below in mind
Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself).
http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/
You can do as below
Example:
new MyClass(ActivityName.this);
class MyClass
{
Context mContext;
public MyClass(Context context)
{
mContext=context;
}
}
pass the context variable through constructor .
create new activity like below
public class GetContext extends AppCompatActivity {
Context mainActivity;
public GetContext(Context mainActivity){
this.mainActivity = mainActivity;
}
and in your previous mainActivity send this context as below
GetContext sendContext = new GetContext(mainActivityContext);
where mainActivityContext is Context mainActivityContext = this;
or simply pass this instead of mainActivityContext
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_attendance_from_database);
}

Calling an activity from another class - Android

How to call an activity from another class to a non-Activity class?
My code is as follows (Activity Class)
public void onCreate(Bundle savedInstanceState){super.onCreateSavedInstanceState);
this.mp();
}
public MediaPlayer mp(){//insert method here// }
Then in my non activity class i call
Intent i = new Intent();
i.setClassName(".......process", ".....ActualRenderingMedia");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
however if I try to use context.startActivity it will give an error asking me to create the activity method. I can't use getApplicationContext.startActivity either.
Is your non-Activity class instantiable? If so, you can add a constructor to the class that accepts a Context Object, and instantiate it from your main Activity.
For example, in you non-Activity class:
public class MyClass {
Context context;
public MyClass(Context context) {
this.context = context;
}
public void someOtherMethod() {
Intent i = new Intent(...);
context.startActivity(i);
}
}
And in your main Activity:
MyClass myclass = new MyClass(this);
...
myclass.someOtherMethod();

Categories

Resources