How can I use classes in Android?

Put your problem here if it does not fit any of the other categories.

How can I use classes in Android?

Postby Addion » Tue Dec 09, 2008 11:46 am

I had a main class and I also created another class for some function. Then, in the main class, I declare an object from my second class. I, however, can't use that object to call some funtion.

public class MainClass extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

SecondClass a = new SecondClass();
a.notifyMe();

}
}

public class SecondClass extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

public void notifyMe() {

final NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.icon, " helpMe",
System.currentTimeMillis());
PendingIntent i=PendingIntent.getActivity(this, 0,
new Intent(this, WindowTest.class),Intent.FLAG_ACTIVITY_NEW_TASK);
note.setLatestEventInfo(this, "Notification Title",
"This is the notification message", i);

note.flags = Notification.FLAG_AUTO_CANCEL;
mgr.notify(5, note);

}

}
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Top

Postby prc » Tue Dec 09, 2008 12:03 pm

Activities are base class for GUI components basically, so you cannot use another activity inside other.



If you wanted to use another activity, you can change it like this, (the bundle here is extra data)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent i = new Intent( this, myTargetActivity.class );
  3.  
  4. Bundle b = new Bundle();
  5.  
  6. b.putString( "key1" , "value1" ); // put string data
  7.  
  8. b.putSerializable( "key2", objectX ); // put any serializable object ( must be cast when read )
  9.  
  10. i.putExtras( b );
  11.  
  12. startActivity( i );
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Also if you dont want to use another Activity, use subAcitivity
----

and reading serialized object

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SomeObj = ( SomeObj ) b.getSerializable( "key2" ); // returns Object, so must cast
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


On the other hand if you need just plain helper class, make a POJO (Plain old java class )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class helperClass
  2.  
  3. {
  4.  
  5.      helperClass()
  6.  
  7.      {
  8.  
  9.      // constructo stuff
  10.  
  11.      }
  12.  
  13.      public void helpMe()
  14.  
  15.      {
  16.  
  17.      System.out.println("No i won't");
  18.  
  19.      }
  20.  
  21. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



and inside activity your activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. helperClass h = new helperClass();
  3.  
  4. h.helpMe();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
prc
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 8:11 am

Postby Addion » Tue Dec 09, 2008 12:52 pm

Thanks for replying me.
I still get confused about your code, but I do more understand what's going on for my code.
I did change on my code, but it still doesn't work. I don't understand about "Bundle". Do I still need it or just options?

Thanks in advance

public class MainClass extends Activity{
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);


Intent i = new Intent( this, SecondClass.class );
// Bundle b = new Bundle();
// b.putString( "key1" , "value1" ); // put string data
// b.putSerializable( "key2", a ); // put any serializable object ( must be cast when read )
// i.putExtras( b );
startActivity( i );

// notifyMe();

}
}
public class SecondClass extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notifyMe();

}

public void notifyMe() {

final NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.icon, " have signal",
System.currentTimeMillis());
PendingIntent i=PendingIntent.getActivity(this, 0,
new Intent(this, WindowTest.class),Intent.FLAG_ACTIVITY_NEW_TASK);
note.setLatestEventInfo(this, "Notification Title",
"This is the notification message", i);

note.flags = Notification.FLAG_AUTO_CANCEL;
mgr.notify(5, note);

}

}
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Postby prc » Tue Dec 09, 2008 4:05 pm

Bundle is basically modified java Map.

It is used for moving data from Activity to another. You do not need to use it, but if you want to pass data, it is best and easiest way.

You can store Serializable objects and basic types to bundle. (Serializable, String, String[], int, int[] ... etc )

I haven't used notifications my self. My app at the moment sends new Intent to gui if it needs some data or information.

For example I have 2 views, one for inserting search key word, and another for showing results. Then I have a binary database that implements its own worker threads for searching.

When my searchGui has some searchword ( string.lenght() > 0 ), it starts resultGui with a new intent, also it commands the DB to do a search.

Every time my DB finds something, it will send a new intent to my resultgui

example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if( newDBresult != null )
  2.  
  3. {
  4.  
  5.     Intent i = new Intent( getApplicationContext() , resultView.classs );
  6.  
  7.     i.setAction( "NewResult" );
  8.  
  9.     Bundle b = new Bundle();
  10.  
  11.     b.putSerializable( newDBresult );
  12.  
  13.     i.putExtras( b );
  14.  
  15.     startActivity( i );
  16.  
  17. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



and on the resultview:

The first intent got from searchview of course starts this view and runs onCreate(). I have declared my resultView to be type of "singleInstance" from the application manifest.xml so any new intent to this view, while it is active is catched on following overridden method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @override
  3.  
  4. public void onNewIntent(Intent intent)
  5.  
  6. {
  7.  
  8.      String action = intent.getAction();
  9.  
  10.      if( action.equals( "newResult" );
  11.  
  12.      {
  13.  
  14.       // store result to list
  15.  
  16.      }
  17.  
  18.      else if( action.equals( "searchComplete" );
  19.  
  20.      {
  21.  
  22.      // use list adapter to show contets of list
  23.  
  24.      }
  25.  
  26. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



Sorry took some time to reply anything, I was in work and they kind of don't like to pay me for answering guestions on opensource dev boards :)

I am guite sure this is not the bestway to do it, and propably using broadcastListener, intentFilter and broadcasting something might be better solution.

I dunno, I found this way working when I started with android and Im not going to refactor my app. :)
prc
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 8:11 am

Postby Addion » Tue Dec 09, 2008 6:46 pm

Thanks so much, though. I will try to work out on my application. I will tell you later about what's going on to my application.
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Postby Addion » Thu Dec 11, 2008 4:13 am

I forgot to register my activity in Manifest. Now I can call another activity from my main activity; however, I have a problem that was my second activity not calling back to main activity. if you have any idea how to fix it, please help me out.



Thanks
Addion
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Top

Postby Addion » Thu Dec 11, 2008 4:43 am

I could finally find out the way to solve my problem. I just added finish(); to my subactivity.

Thanks for all your helps
Addion
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Postby prc » Thu Dec 11, 2008 1:57 pm

No problem.


And you can message from any of your already running activity to another running acitvity atleast by sending new Intent with some data in Bundle if you just override your onNewIntent() on each activity. Not the best or lightest way, but works.
prc
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 8:11 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 23 guests