I am new to Android development. I am trying to join an existing VPN from my Android app. I want to integrate the VPN in my app; my app then is supposed to query a remote database.
I got some code and tried to use it to create a VPN. It emulates the built-in VPN manager on the Android phone. The code compiles and the manager is launched but the connection to the VPN does not succeed when I try to connect after all configuration. The protocol is PPTP. A VPN exists and has been tested.
I tried connecting from an android phone with the same settings and it was successful.
I thought maybe I am passing the parameters in the wrong way. I have put the code for the vpn part below. The url is not the actual but in same format.
Any help to identify what I am doing wrong would be appreciated. Also if there is a way I can directly call the VPN manager from my app.
Thanks very much for any help,
final Button button1 = (Button)findViewById(R.id.button1);
final Button button2 = (Button)findViewById(R.id.button2);
final Button button3 = (Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.net.vpn.SETTINGS"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
String registrationUrl = String.format("daffy.zune.org");
url = new URL(registrationUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Log.d("MyApp", "Registration success");
} else {
Log.w("MyApp", "Registration failed for: " + registrationUrl);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.register);
}
});
}
}
I suggest you look at the simplevpn project and browse the source here and view ShowAllVPNsActivity.java.
Hope this helps! We need more good VPN apps in the Market!
Related
I'm trying to create an app in Android where a user can install zoom.us and Slack apps and run them but I need to check before installation if the app is already installed or not. The problem is I don't know the names of the packages so I can check against them, What would be the name of packages for zoom.us and slack and How would I run them by click of zoom and slack buttons?
public class MainActivity extends AppCompatActivity {
ImageButton zoom, slack;
Button installZoom, installSlack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Install zoom
installZoom = (Button) findViewById(R.id.inst_zoom);
if (isZoomClientInstalled(getApplicationContext())) {
installZoom.setEnabled(false);
} else {
installZoom.setEnabled(true);
installZoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=us.zoom.videomeetings"));
startActivity(intent);
}
});
}
// Run zoom
zoom = (ImageButton) findViewById(R.id.app_zoom);
zoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "About to run zoom", Toast.LENGTH_SHORT).show();
}
});
// Install Slack
installSlack = (Button) findViewById(R.id.inst_slack);
if (isSlckClientInstalled(getApplicationContext())) {
installSlack.setEnabled(false);
} else {
installSlack.setEnabled(true);
installSlack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://slack.com/downloads/android"));
startActivity(intent);
}
});
}
// Run Slack
slack = (ImageButton) findViewById(R.id.app_slack);
slack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "About to run Slack", Toast.LENGTH_SHORT).show();
}
});
}// End of Create();
// Determine whether the zoom for Android client is installed on this device.
public boolean isZoomClientInstalled(Context myContext) {
PackageManager myPackageMgr = myContext.getPackageManager();
try {
myPackageMgr.getPackageInfo("???.???.??", PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
return (false);
}
return (true);
}
// Determine whether the Slack for Android client is installed on this device.
public boolean isSlackClientInstalled(Context myContext) {
PackageManager myPackageMgr = myContext.getPackageManager();
try {
myPackageMgr.getPackageInfo("???.???.??", PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
return (false);
}
return (true);
}
}// End of class
you may find app package by looking into Google Play link
https://play.google.com/store/apps/details?id=com.Slack
com.Slack is package name here. us.zoom.videomeetings for Zoom. Then you just start it with Intent. Try it.
If you know the package name for the app, then you can check that if that app is installed on the device or not.
PACKAGE NAMES:
Zoom.Us: us.zoom.videomeetings
Slack: com.Slack
You know the code for it as stated in the comments. By running it you will know if the app is installed on the device or not.
To check if app is installed or not, you need to know the package name of the app you want to check. You can find the package name of the app from Google play store, focus on URL. Id in URL is the package name.
For Example for Zoom.us it is: us.zoom.videomeetings
Since both apps are well established, it is highly unlikely they will update the package name.
I am currently new to android programming but I recently created a rest api using slim library that takes a GET request and adds it to the database it's parameters but I don't know how to send it inside android app.
here is my code:
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try
{
URLConnection myURLConnection=null;
URL myURL=null;
String mainUrl="http://107.170.31.137/StudentApp/v1/garden/name2/user2/pass2";
myURL = new URL(mainUrl);
myURLConnection = myURL.openConnection();
myURLConnection.connect();
}
catch (IOException e)
{
Toast.makeText(getBaseContext(), "in catch", Toast.LENGTH_LONG).show();
}
}
});
I even added the permissions :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
still the app is not working. am i doing something wrong? please help. also, I want to change the GET parameters every time to the new variables I get from somewhere else how can i do that?
For Network Relational Operations Make Use of the following Libraries(Asyntask)
Retrofit (Recommended)
Okhttp
Volley
Check my Example Project on Retrofit
Here's my situation: i have a server written with sails.js where i have a user model. I have a dashboard where i can see all the users, create new, delete them and so on...Now i want to create an android app where i can get, using socket.io, notification about the events that occour to the user model.
Example: if i delete a user from my dashboard i want the app to recive a notification from the server that the user has been deleted.
Here's my code for the app:
public class MainActivity extends Activity {
//socket instance
private Socket mSocket;
{
try {
mSocket = IO.socket("http://server_url:port/user");
} catch (URISyntaxException e) {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //set the layout for this activity
final TextView tv = (TextView) findViewById(R.id.textView);
mSocket.on("user", new Emitter.Listener() {
#Override
public void call(Object... args) {
tv.setVisibility(View.INVISIBLE);
}
});
mSocket.connect();
final Button btn_createUser = (Button)findViewById(R.id.button);
btn_createUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSocket.connected()) {
//tv.setVisibility(View.INVISIBLE);
}
}
});
}
}
Like i did with the dashboard to connect the socket to the server i did the same thing here, but it seems like the socket doesn't connect, in fact, when i delete a user from the dashboard i get no notification.
Here's the code (that works) i used in my dashboard to connect my socket to the server and listen to updates from the user model:
//connect to the server and listen to updates from user model
io.socket.get('/user', function(data, jwres) {
$scope.users = data;
$scope.$apply(); //write users in the table...
});
//wait for updates...
io.socket.on('user', function serverResponded (data) {
if(data.verb == "updated")
{
//get the user index in the array and update it
var index = getIndex(data.id, $scope.users);
$scope.users[index].online = data.data.online;
$scope.$apply();
}
if(data.verb == "destroyed")
{
//get the user index in the array and remove it
var index = getIndex(data.id, $scope.users);
$scope.users.splice(index, 1);
$scope.$apply();
}
if(data.verb == "created")
{
//simply update the array
$scope.users.push(data.data);
$scope.$apply();
}
});
Now, i think all i'm missing out in the android app is the GET request which automatically subscribe my socket to the model and get notified if something happen...but i don't know hot to do it.
I hope i was clear enough...thank to everyone who will answer me!
PS: i don't want to use AndroidAsync because i need the ultimate version of socket.io!
In case someone need it, i found the solution: https://github.com/balderdashy/sails/issues/2640 here you can find out how to solve the issue! :)
I'm working on a relatively simple login app for a school project. I'm having an issue connecting my app to any URL, local or otherwise. I've added the permission for internet access to the android manifest file.
<uses-permission android:name="android.permission.INTERNET" />
The following code is located in my main activity:
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.ucidText);
password = (EditText)findViewById(R.id.passText);
login = (Button)findViewById(R.id.button1);
//URL CAN BE ANYTHING, WOULD NORMALLY BE 192.168.1.102
if(isConnectedToServer("<-URL to page on locally hosted server->",10)){
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show();
}
}
//Function to determine if connection exists
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url); //<--------- I believe the issue has to do with this line.
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
return false;
}
}
}
This is a stripped down version of my overall project. Everything checks out except the connectivity. I'm running the app from the SDK straight on my phone (Moto X). Any suggestions?
You got the android.os.NetworkOnMainThreadException because you perform your network request on the main thread. You should not do that because you will block the main thread. Instead, you can start a new thread to perform your network request, like this:
final Handler handler = new Handler();
new Thread(){
public void run(){
// check network connection here.
handler.post(/* do the toast work in a Runnable as the parameter here*/);
}
}.start();
You should use some protocol in the "url" string.
For example, you can use
"http://serverIP"
in which the serverIP is 192.168.1.102 and the port is default to 80.
We could not use "192.168.1.102" as the url because the protocol could not be ignored for the URL class.
Hi I am in need of some help I am working on an app where I want the to user click a button and the phone reboots. My problem is when I click the button it gives a super user request but does not reboot. My code is:
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
}
});
}
}
Is there anything I am doing wrong? If anyone could help i would really appreciate it.
You create two different shells this way. Assign the process to some variable and grab its IO streams:
Process p = Runtime.getRuntime().exec("su");
InputStream is = p.getInputStream();
// ...
Then write the command directly.
Note that this will not work on unrooted device. Avoid this if possible.