I have five domain server and i want to connect to that server using java socket programming in my android application.
Here my servers
"ANY1.g%%%%%%%%.dk"
"ANY2.g%%%%%%%%.dk"
"ANY3.g%%%%%%%%.dk"
"ANY4.g%%%%%%%%.dk"
"ANY5.g%%%%%%%%.dk"
So when I ping to any of server via ping command in run prompt, it is working perfect and getting response.
but when i am trying to crate socket using this server it gives me "Unknown Host" error.
Here is my code:
try
{
hostname = "ANY SERVER DOMAIN NAME";
port = ANY;
socket = new Socket(hostname, port);
socket.setReceiveBufferSize(2048);
socket.setKeepAlive(true);
Log.e("SOCKET",socket.isConnected());
}
catch(Exception e)
{
Log.e("ERROR",e.getMessage());
}
Is there any problem with code or any suggestion will be accepted?
Menifest file :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.siliconithub.android.greencall"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_app_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".GreenCall"
android:label="#string/title_activity_green_call"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RecentCallActivity"
android:label="#string/title_activity_recentcall"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".RecentCallDetailActivity"
android:label="#string/title_activity_recentcall_detail"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".FavouritesActivity"
android:label="#string/title_activity_fovourites"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".ContactsActivity"
android:label="#string/title_activity_contacts"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".ContactDetailsActivity"
android:label="#string/title_activity_contactdetails"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".TelnetServerActivity"
android:label="#string/title_activity_telnetserver"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".SettingActivity"
android:label="#string/title_activity_setting"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black" >
</activity>
<activity
android:name=".SearchContactsActivity"
android:label="#string/title_activity_search_contacts"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<service
android:name=".UpdateStatusService"
android:label="#string/title_service_update_status"
android:icon="#drawable/ic_app_icon"
android:enabled="true" />
<receiver android:name=".PhoneStateBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
thank you.
import java.net.Socket;
public class Main {
public static void main(String[] args) {
try {
Socket socket = new Socket("%%%%.%%%%%.dk", PORT NUMBER);
socket.setReceiveBufferSize(2048);
socket.setKeepAlive(true);
System.out.println("socket " + socket.isConnected());
} catch (Exception e) {
System.out.println("error " + e.getMessage());
}
}
}
This returns
socket true
In Java (no Android). It also works in Android (sherpya has tested it - see his comment below). I guess there is something wrong with your permissions. Can you show us your manifest?
The best way is to use an InetAddress+ SocketAddress to resolve the host, then connect to it using a socket.
Try:
InetAddress addr = InetAddress.getByName("ANY1.g%%%%%%%.dk");
SocketAddress sockaddr = new InetSocketAddress(addr, 443);
socket = new Socket();
socket.connect(sockaddr, timeoutint)
Related
I'm trying to put an app on google and they reject it in addition to not giving me a concrete answer.
It's an SMS-sending app. I don't keep any user data.
The user fills in 3 fields and is sent to a predefined toll-free number, which sends a reply.
The part of the number and the answer is not up to the app: it is just to facilitate the user's work.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#drawable/tres"
android:label="#string/app_name"
android:roundIcon="#drawable/tres"
android:supportsRtl="true"
android:theme="#style/Theme.FieldLocalize"
tools:targetApi="31">
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-4545343114224253~3369918696"/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
private void requerirPermissao() {
if (!permissaoConcedida()) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.SEND_SMS}, 1);
}
}
private boolean permissaoConcedida() {
return ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED;
}
Sadly, Android 11 storage restrictions are too hard and confusing.
I'm trying to pick a document using the below code
fun onFile(activity: AppCompatActivity?, fragmentContext: Fragment?, isMultiple: Boolean?, pFileMimeTypes: Array<String>?): Intent {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
action = Intent.ACTION_OPEN_DOCUMENT
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, isMultiple ?: false)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
val fileMimeTypes = pFileMimeTypes ?: FileUtil.mimeTypes
intent.type = if (fileMimeTypes.size == 1) fileMimeTypes[0] else "*/*"
if (!fileMimeTypes.isNullOrEmpty()) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, fileMimeTypes)
}
val list = activity?.packageManager?.queryIntentActivities(intent, PackageManager.MATCH_ALL)
if (list?.size!! > 0) {
if (fragmentContext == null) {
activity.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
} else {
fragmentContext.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
}
}
// returning intent is not being used anywhere else
return intent
}
Although I have all files access permission Environment.isExternalStorageManager() but still after selecting document(PDF) from the external storage (download or any folder), OnActvityResult is being called with result code 0. I need the Uri of the selected document to proceed further.
For any other reference, below is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="*****Hidden*******">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_STORAGE_PERMISSION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:exported="true"
android:name=".Main2Activity"
android:launchMode="singleTask"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.attachmentmanager"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider"
tools:replace="android:resource" />
</provider>
</application>
<queries>
<intent>
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.PICK" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
</queries>
</manifest>
I must be missing some point. Everything is working fine below Android 11
Thanks in Advance.
startActivityForResult is deprecated
Refer this link
Use Activity Results API
Official Docs
I have been implementing the code of login and registration app from http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
I copy pasted the code along with following the instructions but I am still getting the registration error : null in the log cat on pressing the register button.
The php files(ex: register.php) are displaying {"error":true,"error_msg":"Required parameters (name, email or password) is missing!"} , that means they are working fine.
I am using xampp and have kept the files in htdocs. I have checked various url in Appconfig.java such as 127.0.0.1, 10.0.2.2:8080, 10.0.2.2:80, and the ip of my computer system from ipconfig cmd, but still getting same error.
Here is the code of Appconfig.java.
package com.urjapawar.hackathon1;
public class AppConfig {
// Server user login url
public static String URL_LOGIN ="http://192.168.2.5/android_login_api/login.php";
// Server user register url
public static String URL_REGISTER = "http://192.168.2.5/android_login_api/register.php";
}
and here is the code of Android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.urjapawar.hackathon1" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.urjapawar.hackathon1.AppController"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".RegisterActivity"
android:label="#string/title_activity_register"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
</activity>
</application>
Also I have checked correctly username and password in config.php, Please help.
I just need to replace 192.168.2.5 with 192.168.2.5:80
I am trying to use gcm to push notifications on android devices. So far I have registered with gcm and sent my back-end the necessary information to make a post request to the gcm severs which builds a 200 response. All the steps have come together except the client side receiving the message. I don't get the message. Displayed here is my gcm listener class:
import com.google.android.gms.gcm.GcmListenerService;
public class MyGcmListenerService extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
private static final String TAG = "MyGcmListenerService";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(from,message);
}
private void sendNotification(String title, String body) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title)
.setContentText(body);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<recieve android:name = ".GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.caesar.gcm" />
</intent-filter>
</recieve>
</application>
// new manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<reciever
android:name = "com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.caesar.gcm" />
</intent-filter>
</reciever>
<service
android:name="com.example.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
</application>
Actually looking at this further it looks like you haven't setup the receiver correctly. Seems you need to include at least.
<service
android:name="com.example.MyGcmListenerService "
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
And for the rest just follow that link.
You have to put your resgistration intent service at the end of yout Manifest file. Like this example:
<service
android:name="com.example.gcm.RegistrationIntentService"
android:exported="false">
</service>
Hope that help you, more information, you can visit this link and you can view an example of RegistrationIntentService here.
I'm using GCM in my android application and when I try to register the device with GCM (with the command gcm.register(SENDER_ID)) I get the SEVICE_NOT_AVAILABLE error, I tried what someone suggested here (the accepted solution) and I did get the registration id.
Why is it like that?
Android manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appspot.smartgan"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.appspot.smartgan.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.appspot.smartgan.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>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.appspot.smartgan" />
</intent-filter>
</receiver>
<service
android:name=".GcmIntentService"
android:enabled="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.appspot.smartgan.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name="com.appspot.smartgan.ChildActivity"
android:label="#string/title_activity_child" >
</activity>
</application>
</manifest>
register method:
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String message = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
message = "Device registered, registration ID=" + regid;
Log.d("SMARTGAN_PLAY", "register completed");
// send registration id to the server
sendRegistrationIdToServer();
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException e) {
message = "Error:" + e.getMessage();
}
return message;
}
}.execute(null, null, null);
}
I think issue is regarding allowed IP addresses mentioned in your Google API project.
Please check details of the project and remove IP address if present under restrict use to IP address. I faced similar problem.
If you are receiving a Register ID it means you have registered the device successfully to the GCM.
Edit:
Here is a good tutorial for GCM: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
<receiver
android:name="rockit.app.beardallstreetprimary.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="rockit.app.beardallstreetprimary" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gcm.GCMIntentService" android:enabled="true" />
My GCM is set up based on the Tutorial i linked above so if you read through that aswell it should work for you it looks like your not far off. I will say the Manifest is very picky and you need to ensure that you link your Packages fully.
Also you dont need the Register Intent