integrate yahoo messenger with android app - android

i surfed net to find some code snippet or some help to integrate yahoo messenger in android application. i could not find some thing helpful.
Please Help me by providing some link, tutorial or code sample so that i could integrate the yahoo messenger.
Thanks

Finally got solution for yahoo messenger integration i used openymsg a library and using below link
http://edwin.baculsoft.com/2011/11/creating-a-simple-yahoo-messenger-auto-response-with-java-and-openymsg-library/
changed my code in activity as below worked like a charm :)
public class SimpleYahoo extends Activity implements SessionListener{
/** Called when the activity is first created. */
private Logger logger = Logger.getAnonymousLogger();
private Session session = new Session();
Button sendButton;
boolean isLoginsuccess;
ListView resultTextView;
EditText editText;
Handler handler;
ArrayList<String> replymessage;
String reply;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replymessage=new ArrayList<String>();
try {
session.login("yahooid", "password");
} catch (AccountLockedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LoginRefusedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FailedLoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.addSessionListener(this);
sendButton=(Button) findViewById(R.id.button);
resultTextView=(ListView) findViewById(R.id.result);
editText=(EditText) findViewById(R.id.input);
sendButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
doLogin();
}
});
handler=new Handler()
{
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what==0)
{
replymessage.add(reply);
setListAdapter();
}
}
};
}
private void doLogin() {
try {
// insert your yahoo id
// as for this example, im using my yahoo ID "dombaganas"
session.sendMessage("targetyahooid", editText.getText().toString());
} catch (Exception e) {
Log.e(e.getMessage(), e.getMessage());
}
}
#Override
public void dispatch(FireEvent fe) {
// TODO Auto-generated method stub
ServiceType type = fe.getType();
SessionEvent sessionEvent = fe.getEvent();
if (type == ServiceType.MESSAGE) {
try {
// log request message
reply=sessionEvent.getMessage();
Log.i("message","message from " + sessionEvent.getFrom() + " \nmessage " + sessionEvent.getMessage() );
// give an automatic response
// session.sendMessage(sessionEvent.getFrom(), "hi, you are sending " + sessionEvent.getMessage());
// session.
handler.sendEmptyMessage(0);
} catch (Exception e) {
Log.e(e.getMessage(), e.getMessage());
}
}
}
public void setListAdapter()
{
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,R.layout.multi_line_list_item,replymessage);
resultTextView.setAdapter(arrayAdapter);
resultTextView.setSelection(replymessage.size()-1);
}
}

Related

mediaplayer.prepare throws IllegalStateException in android 6

I have a code that working in every android version.
but in android 6 throws
IllegalStateException
when i am calling
mediaPlayer.prepare();.
first page is working very well, but when i scroll to another pages, some time working very well and some time it is throwing IllegalStateException and audio can't play.
it is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mp3_player);
pager = (ViewPager) findViewById(R.id.pager);
Bundle extras = getIntent().getExtras();
final int id = extras.getInt("ID");
auMp3 = id + "_1.mp3";
f = new File(DIR_DATABASE + auMp3);
Uri uri = Uri.parse(G.DIR_DATABASE + auMp3);
if (f.exists()) {
MediaPlayer mp = MediaPlayer.create(G.context, uri);
}
imgPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (f.exists()) {
if (mp.isPlaying()) {
imgPlay.setImageResource(R.drawable.iplay);
mp.pause();
} else if ( !mp.isPlaying()) {
imgPlay.setImageResource(R.drawable.ipause);
mp.start();
}
} else {
menu.download(auMp3);
}
}
});
adapter = new AuPagerAdapter(aue);
pager.setAdapter(adapter);
pager.setCurrentItem(aue.size());
OnPageChangeListener currentit = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
int i = aue.size() - arg0;
auMp3 = id + "_" + i + ".mp3";
f = new File(DIR_DATABASE + auMp3);
imgPlay.setImageResource(R.drawable.iplay);
mp.reset();
if (f.exists()) {
try {
mp.setDataSource(G.DIR_DATABASE + auMp3);
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepareAsync();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(G.context, "IllegalStateException", 1).show();
}
}
}

Android handling state of ProgressBar onPause/onResume

Hi have a progressBar that i run. but when the user exits the app via the home button and then re-enters it by holding down the home button and selected the app. The progressBar setProgress flickers between an old value and a new one almost as if its remembering the previous state. I have created a function in my on pause/onResume methods to pause the timer save the values and on resume get those values and start the timer and progress bar again. The confusing thing is it works the onPause stuff works perfectly if the onPause function is run and the user stays in the app for example presses the back button. Does anyone know where im going wrong or how to correctly implement a progressBar using a CountDownTimer?
heres my code
#Override
protected void onResume() {
// TODO Auto-generated method stub
Log.v("Game","ONRESUME");
loadData();
layoutChecks();
loadViews();
try {
if(!myData.get("completed").equals("3")){
hintCheck();
getHints();
startTimer();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onResume();
}
#Override
protected void onPause() {
Log.v("Game","ONPAUSE");
try {
if(!myData.get("completed").equals("3")){
pauseTimer();
updateData();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPause();
}
public void loadViews(){
progressBar = new ProgressBar(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressD = progressBar.getProgressDrawable();
}
public void prepareProgressBar(){
progressD.setLevel(0);
progressBar.setProgress(0);
progressD.setLevel(points*100);
progressBar.setProgress(points);
}
public void startTimer(){
try {
points = Integer.valueOf(myData.getJSONObject("Data").getString("points"));
int timer = points;
hasTimerCancelled = false;
prepareProgressBar();
counter = new MyCount(timer/2 * 1000,1000);
counter.start();
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void pauseTimer(){
try {
counter.cancel();
hasTimerCancelled = true;
JSONObject Data = myData.getJSONObject("Data");
Data.put("points", String.valueOf(points));
myData.put("Data", Data);
updateData();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class MyCount extends CountDownTimer{
public long totaltime;
public MyCount(long totalTime, long countDownInterval) {
super(totalTime, countDownInterval);
totaltime = totalTime;
}
#Override
public void onFinish() {
counter.cancel();
points = 0;
progressBar.setProgress(0);
}
#Override
public void onTick(long millisUntilFinished) {
if(!hasTimerCancelled){
if(points > 0){
points = points - 1;
}
progressBar.setProgress((int) totaltime);
}
}
}

error in using android-facebook-sdk

I am just not able to figure out the problem in my code
I want to login with FaceBook for my app
I have followed a tutorial from a youtube video.
in the code , I am able to login using facebook, but I am not able to implement the logout functionality.
i tried doing it. (the code for logging out is also there but still its not working)
the code is as follows:
after i click on login button:
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.fb_button:
try{
//to check if already logged in.
//my code never enters the following block.
if(fb.isSessionValid()){
Toast.makeText(getBaseContext(), "loging out", Toast.LENGTH_LONG).show();
try {
fb.logout(getBaseContext());
// update_fb_buttonimage() is a function to change the image of button
//if logged in: it shows logout
// else it shows login.
//but somehow its not working
update_fb_buttonimage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//following is working fine.
// but every time i click n the button, it enters this bolok of code.
//which is not what i want. this is tobe executed only if user is not logged in
//even if user is logged in , program enters this code only.
// instead of this th above "if" block should be executed.
else{
Toast.makeText(getBaseContext(), "logging in", Toast.LENGTH_LONG).show();
fb.authorize(LoginPage.this,new DialogListener(){
#Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
update_fb_buttonimage();
}
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You can't login to the facebook", Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "unIdentified Error", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onCancel", Toast.LENGTH_LONG).show();
}} );
} }catch(Exception e){
System.out.print(e.toString());
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
break;
}
Where is my error?
just replace the MainActivity.this with yours and updateButtonImage()
if(fb.isSessionValid())
{
try {
fb.logout(getApplicationContext());
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//button close session
}
else
{
fb.authorize(MainActivity.this,new String[] {"publish_stream"}, new DialogListener(){
#Override
public void onFacebookError(FacebookError e)
{
Toast.makeText(MainActivity.this, "on Facebook error", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(DialogError e)
{
Toast.makeText(MainActivity.this, "on error", Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete(Bundle values)
{
updateButtonImage();
}
#Override
public void onCancel()
{
}
});
//login in to facebook
}

AsyncTask Execution Debugging

I have used ASyncTask before and have not faced any problems. I don't understand why this code is not working. I have to access an ftp server to download a text file that i set to a TextView. The code within the ASyncTask works fine. I have checked it. For some reason I am getting a problem when I call the new thread 'new GetStory().execute'. Any help would be great.
package com.amazingstories;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayStory extends Activity {
String path;
TextView story;
FTPClient ftp = new FTPClient();
File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
path = getIntent().getExtras().getString("path");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.displaystory);
story = (TextView) findViewById(R.id.tvStory);
story.setText(path);
file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"temp.txt");
new GetStory().execute();
// try{
// new GetStory().execute();
// }catch(Exception e){
// Toast.makeText(getApplicationContext(), e.toString(),
// Toast.LENGTH_SHORT).show();
//
// }
}
public class GetStory extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
try {
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
if (!ftp.isConnected()) {
ftp.connect(".....");
Toast.makeText(getApplicationContext(),
"connected to server", Toast.LENGTH_SHORT).show();
ftp.login("....", ".....");
}
Toast.makeText(getApplicationContext(), "FTP Connected",
Toast.LENGTH_SHORT).show();
ftp.changeDirectoryUp();
ftp.download(path, file, new FTPDataTransferListener() {
//ProgressDialog dialog;
#Override
public void aborted() {
// TODO Auto-generated method stub
// dialog.dismiss();
story.setText("Transfer Aborted");
}
#Override
public void completed() {
// TODO Auto-generated method stub
//dialog.dismiss();
}
#Override
public void failed() {
// TODO Auto-generated method stub
//dialog.dismiss();
story.setText("Transfer Failed");
}
#Override
public void started() {
// TODO Auto-generated method stub
//dialog = ProgressDialog.show(DisplayStory.this, "",
// "Loading. Please wait...", true);
}
#Override
public void transferred(int arg0) {
// TODO Auto-generated method stub
}
});
// tv.setText(test.toString());
Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_SHORT).show();
} catch (FTPException e) {
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPDataTransferException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),
"Exception Caught " + e, Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (FTPAbortedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
ftp.logout();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FTPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
getFileData get = new getFileData();
try {
story.setText(get.getData(file));
Toast.makeText(getApplicationContext(), "Set TextView",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
file.delete();
}
}
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Try",
Toast.LENGTH_SHORT).show();
You are not allowed to call Toast inside doInBackground.
doInBackground method runs in non-ui thread so you can not manipulate or sidplay Toast on UI.It must be done in onPreExecute or in onPostExceute.
The methods onPreExecute(), onPostExecute(), and onProgressUpdate() are the only methods during an AsyncTask execution that are called on the main (UI) thread, and therefore are the only places you can directly make calls to UI methods (like TextView.setText() and Toast.makeText()).
Your program is likely crashing because you cannot create a Toast from a thread where "a Looper has not been prepared", which is what happends when you do so from doInBackground(); this causes an exception and a crash.

Android JSch session closes after two commands

I'm attempting to run some commands over ssh from my app. I have a door unlocker and a script that takes lock or unlock.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable()
{
public void run()
{
connectSSH();
}
}).start();
}
I setup the session in a separate thread as you cannot do networking in the main UI thread on android.
private void connectSSH()
{
JSch jsch = new JSch();
try {
session = jsch.getSession("door", "my server",22);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setPassword("pass");
session.setConfig("StrictHostKeyChecking", "no");
try {
session.connect(40000);
channel = session.openChannel("exec");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
From here I just have button listeners:
public void unlock(View view) {
((ChannelExec)channel).setCommand("python /home/door/doorUnlocker.py unlock");
try {
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.disconnect();
}
public void lock(View view) {
((ChannelExec)channel).setCommand("python /home/door/doorUnlocker.py lock");
try {
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.disconnect();
}
I can unlock, then lock the door but after that the session just closes so when it attempts to connect to the channel it throws an exception.
Thanks for any help you can provide.

Categories

Resources