I have a simple code which will show "Available" and "Unavailable" using asmack api, but whenever i click on the button it throws a message of not being connected to server. I am posting my code below.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usingxmpp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.usingxmpp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/string_head" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button1"
android:onClick="onClick1"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button2"
android:onClick="onClick2"
/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Usingxmpp</string>
<string name="action_settings">Settings</string>
<string name="string_head">Hello world!</string>
<string name="button1">Avail</string>
<string name="button2">Unavail</string>
</resources>
MainActivity.java
package com.example.usingxmpp;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
ConnectionConfiguration connfig = new ConnectionConfiguration("acer-PC",5222,"acer-pc");
XMPPConnection connection = new XMPPConnection(connfig);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
connection.connect();
connection.login("rahul","rahul");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void onClick1(View v)
{
new Thread(new Runnable()
{
public void run()
{
try
{
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("I am available");
connection.sendPacket(presence);
//String status = presence.getStatus();
Toast.makeText(getBaseContext(), "You are Available!", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
).start();
}
public void onClick2(View v)
{
new Thread(new Runnable()
{
public void run()
{
try
{
Presence presence = new Presence(Presence.Type.unavailable);
presence.setStatus("I am unavailable");
connection.sendPacket(presence);
//String status = presence.getStatus();
Toast.makeText(getBaseContext(), "UnAvailable now!", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
).start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat
12-02 14:41:40.809: W/System.err(5464): java.lang.IllegalStateException: Not connected to server.
12-02 14:41:40.839: W/System.err(5464): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:194)
12-02 14:41:40.839: W/System.err(5464): at org.jivesoftware.smack.Connection.login(Connection.java:355)
12-02 14:41:40.839: W/System.err(5464): at com.example.usingxmpp.MainActivity$1.run(MainActivity.java:44)
12-02 14:41:40.839: W/System.err(5464): at java.lang.Thread.run(Thread.java:856)
When I press Avail button the above errors comes in logcat.
Please help me in solving this problem of connecting to server.
Thanks
(Note: Runnning this project on my device)
The problem is this code sequence:
connection.login("rahul","rahul");
connection.connect();
You need to be connected before you can login. Sounds reasonable, does it? Therefore you need to change the code to:
connection.connect();
connection.login("rahul","rahul");
It could be because your android xmpp client is not able to connect to your host (in your case acer-PC). I mentioned the IP address for host on which the xmpp server is installed and it worked for me. You have to be careful with IP address, you may directly use your local WIFI IP address, you will be able to connect to xmpp server from android device if both your PC & android device are using the same WIFI. In case if you are using External IP address you may have to do some additional configurations as discussed here in this link
Related
I am creating an app to check whether the given address of a server is valid or not. It sends request to a server when Connect button is pressed. If request succeed it is valid if not invalid ...
But I am having an error when I run a thread. I cant run it on Computer caz having issues enabling ADV but here is my code.
Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Server Address"
app:layout_constraintBottom_toTopOf="#+id/addressInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.869" />
<EditText
android:id="#+id/addressInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Enter your server address"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/addressBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
android:onClick="onAddressButtonClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/addressInput"
app:layout_constraintVertical_bias="0.065" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.naveed.youtubestream;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button addressBtn;
private EditText addressInput;
private String serverAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressBtn = findViewById(R.id.addressBtn);
addressInput = findViewById(R.id.addressInput);
}
private boolean checkAddress(String address){
try {
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
return true;
}
} catch (IOException e) { }
return false;
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
try{
if(checkAddress(serverAddress)){
//TODO --> Set new Layout
Toast.makeText(getApplicationContext(), "Server address was valid.", Toast.LENGTH_LONG).show();
}
else {
addressInput.setText("");
Toast.makeText(getApplicationContext(), "Invalid server address.", Toast.LENGTH_LONG).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
public void onAddressButtonClick(View view) {
try{
serverAddress = addressInput.getText().toString();
Thread thread = new Thread(runnable);
thread.start();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.naveed.youtubestream">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm trying to deploy my first test app, but it's giving me a Parse Error - There was a problem while parsing the package.
Whilst, it executes fine on my Genymotion virtual device. It's the normal first "Hello World" app you'd get automatically.
Also, it executes on both virtual devices of Samsung Galaxy S3 and Samsung Galaxy S5 but not on real devices such as Samsung Galaxy S5 and Samsung Galaxy Note 10.1.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lovely"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main activity:
package com.example.lovely;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lovely.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Appreciate any assistance, thank you.
Exporting as signed rather than unsigned fixed it. Not sure why, my phone and tablet both accept " unknown sources " feel free to explain why this has hapenned
i am working on a network communication project in android, but my app dose not work ! after some debugging, i find out that every network request has a null response from system.
i tried so many ways such as :
1) hard restart my phone
2) adding all network permissions to the manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
3) using local network ( my phone as a server )
4) letting all applications to connect to the network : setting > networked apps > ...
but ! nothing happened...
this is my similar problem link, but there is no answer in this topic:
can't connect to internet in ONLY in my android app. Each method to connect internet returns null
network is working on my phone because some apps like telegram and chrome are working well !
and there is a funny thing here, when i copy and past this code to the simple java project not android project, it works well !
because i wanted to find out where is the problem, i just copy 3 simple lines of codes to the simple android project, but still nothing happened!
manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dltests"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
source file
package com.example.dltests;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextView = (TextView) findViewById(R.id.hello);
try
{
URL mUrl = new URL("http://192.168.1.53:8080/some.zip");
HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
mTextView.setText(mHttpURLConnection.getContentLength());
}
catch (Exception e)
{
mTextView.setText("ERROR: " + e.getMessage());
}
}
}
and the result is :
ERROR: null
Android bars you from networking in the UI thread. What you need to do it spawn a separate thread to do the networking for you. Then you need either a receiver or handler to post/update the UI with the data.
1 - Create a new class:
import android.os.Handler;
import android.os.Message;
public class GetData extends Thread
{
// Holds url that data is stored on
private URL url
// Sets the url value
public GetData(URL newUrl)
{
url = newUrl;
}
// Runs when the thread is launched
public void run()
{
// Opens connection
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Sends back to UI using a Message
Message msg = MainActivity.MainActivityInterface.obtainMessage(MainActivity.getAndUpdate);
msg.obj = con.getContentLength(); // Adds the data
HomeScreen.homeScreenInterface.sendMessage(msg);
}
}
2 - Create handler in activity so UI can be updated as well as launch worker thread
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
public class MainActivity extends Activity {
// Holds activity instance
public static MainActivity currentInstance;
// Variable that allows us to separate messages for handler
public static final getAndUpdate = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentInstance = this;
TextView mTextView = (TextView) findViewById(R.id.hello);
try
{
URL mUrl = new URL("http://192.168.1.53:8080/some.zip");
// Spawns worker thread
GetData gd = new GetData(mUrl);
gd.start();
}
catch (Exception e)
{
mTextView.setText("ERROR: " + e.getMessage());
}
}
// Deals with service responses
public static Handler MainActivityInterface = new Handler()
{
#Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case getAndUpdate:
currentInstance.mTextView.setText((String) msg.obj); // Gets message object and updates UI
break;
}
}
}
}
I hope this helps. Note that I have not tested it as I am very far away from my coding computer!
I'm beginner in Android development and I'm trying to make Android app which will send SMS to another phone when button is clicked. I don't have Android phone so I'm trying to do that via emulators. I start one emulator which will be my "phone" which send SMS and another emulator which receives SMS.
Everything is compiled well and app starts, but when I click on button, I got message that "Unfortunately, app has stopped".
Here is the code which I have made.
acitivity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.email.MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="44dp"
android:text="Button" />
and here is MainActivity.java
package com.example.email;
import com.example.email.R;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//adding listener to the button
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String phoneNumber = "5556";
String message = "Proba";
//sends SMS to emulator with number 5556
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
});
}
//this is default function, I haven't changed anything
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.email"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
add
<uses-permission android:name="android.permission.SEND_SMS" />
in your manifest.xml
-I have created an android app that has 2 buttons to download apks from internet server if not already installed on the device.
-I want to provide shortcut icons of these newly installed apps in my application.
I've seen a posts about launcher apps but they list out shortcuts for all apps. I want specifically for my 2 apps.
Is there a way to do it?
here's the code for existing app.
//MainActivity.java
package com.example.appcommunication;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button app1, app2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
app1 = (Button) findViewById(R.id.button1);
app2 = (Button) findViewById(R.id.button2);
final boolean app1Installed = appInstalledOrNot("com.example.cameraphonegap");
final boolean app2Installed = appInstalledOrNot("com.example.webviewexample");
if(app1Installed){
app1.setEnabled(false);
app1.setText("App1 Installed ");
}else{
app1.setEnabled(true);
}
if(app2Installed){
app2.setEnabled(false);
app2.setText("App2 Installed ");
}else{
app2.setEnabled(true);
}
app1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("App is not installed on your phone");
Log.d("NOT installed","App not installed on your phone");
Toast.makeText(getApplicationContext(), "App1 Not Installed ", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.dropbox.com/s/9vyvypj9qxjanb/CameraPhoneGap.apk"));
startActivity(browserIntent);
}
});
app2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("App is not installed on your phone");
Log.d("NOT installed","App not installed on your phone");
Toast.makeText(getApplicationContext(), "App2 Not Installed ", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.dropbox.com/s/r2z299661ibmhm/webviewExample.apk"));
startActivity(browserIntent);
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity"
android:background="#7FFFD4">
<Button
android:id="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Install app1"
android:textStyle="bold"
/>
<Button
android:id="#+id/button2"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Install app2"
android:textStyle="bold"
/>
</LinearLayout>
//Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appcommunication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.appcommunication.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I implemented a workaround by saving the ic_launcher of these apps and using it as ImageButtons for launching the apps. But I need something automatic for the icons when I use the packageName of the apps..
//code for ImageButton:
ImageButton launchApp1=(ImageButton) findViewById(R.id.button3);
launchApp1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(app1Installed){
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.example.cameraphonegap");
startActivity(LaunchIntent);}else{
Log.d("installed","App1 not installed on your phone");
Toast.makeText(getApplicationContext(),
"App1 Not Installed ", Toast.LENGTH_SHORT).show(); }
}
});
// activity_main.xml
<ImageButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/button1"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:background="#null"
android:contentDescription="#null"
android:src="#drawable/app1" />
How about serving these images from the server as well? You can host the images(key values for the package name?) for these apps somewhere and list them inside a listview on your app?
This is the exact thing you want to implement but using a different method to achieve it then what you are trying to do.