Button link to facebook application - java

I have a button link to facebook account but it connect to Google browser in my mobile (as shown) but I don't want that, I want it to take me to facebook application in my mobile and if it doesn't exist, it takes me to Google play application to download/install facebook, does anybody know how?
package com.el.dom;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class PageAb extends Activity {
Button dclink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ab);
addListenerOnButton();
}
public void addListenerOnButton() {
dclink= (Button) findViewById(R.id.dctec);
dclink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/blah"));
startActivity(browserIntent);
}
});
}
}

use android facebook sdk create an app at http://developers.facebook.com/ and proceed

try {
//try to open page in facebook native app.
String uri = "fb://page/" + yourFBpageId; //Cutsom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "http://touch.facebook.com/pages/x/" + yourFBpageId; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uriMobile));
startActivity(i);
}
copied from How to navigate user to a facebook page via Android's facebook api?

Related

I am trying to send data from Browser to Android mobile App using STOMP and is facing issue with IP address

I am pretty new to STOMP, just learning it for a few weeks only. I have been trying for some time to send data from Browser to Android mobile App using STOMP, so far I have been successful in sending data from my browser to Android emulator, however if I try it on my mobile phone, the app will crash.
What I did:
I ran the Spring Boot Websocket Server before running the Android studio emulator
Used the same wifi for my phone and laptop where the server is run on.
My code is based off of xlui/WebSocketExample. (Search on google and you can find the code)
I am using NaikSoftware/StompProtocolAndroid in Android Studio, the Android client uses StompProtocolAndroid which implements protocol STOMP on Android, to subscribe or send message to server.
The Spring Boot WebSocket Server is ran on Intelliji full Version.
changed from 10.0.2.2/8080/im/websocket to Stomp.over(Stomp.ConnectionProvider.OKHTTP, "ws://MY LAPTOP IP ADDRESS/8080/im/websocket");
My Code in Android Studio for the Broadcast activity:
package app.xlui.example.im.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
import app.xlui.example.im.R;
import app.xlui.example.im.conf.Const;
import app.xlui.example.im.util.StompUtils;
import ua.naiksoftware.stomp.Stomp;
import ua.naiksoftware.stomp.StompClient;
import ua.naiksoftware.stomp.dto.StompCommand;
import ua.naiksoftware.stomp.dto.StompHeader;
import ua.naiksoftware.stomp.dto.StompMessage;
#SuppressWarnings({"FieldCanBeLocal", "ResultOfMethodCallIgnored", "CheckResult"})
public class BroadcastActivity extends AppCompatActivity {
private Button broadcastButton;
private Button groupButton;
private Button chatButton;
private EditText nameText;
private Button sendButton;
private TextView resultText;
private EditText nameput;
private void init() {
broadcastButton = findViewById(R.id.broadcast);
broadcastButton.setEnabled(false);
groupButton = findViewById(R.id.groups);
chatButton = findViewById(R.id.chat);
nameText = findViewById(R.id.name);
sendButton = findViewById(R.id.send);
resultText = findViewById(R.id.show);
nameput=findViewById(R.id.name2);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast);
this.init();
StompClient stompClient = Stomp.over(Stomp.ConnectionProvider.OKHTTP, "ws://MY LAPTOP IP ADDRESS/im/websocket");
StompUtils.lifecycle(stompClient);
Toast.makeText(this, "Start connecting to server", Toast.LENGTH_SHORT).show();
// Connect to WebSocket server
stompClient.connect();
// 订阅消息
Log.i(Const.TAG, "Subscribe broadcast endpoint to receive response");
stompClient.topic(Const.broadcastResponse).subscribe(stompMessage -> {
JSONObject jsonObject = new JSONObject(stompMessage.getPayload());
Log.i(Const.TAG, "Receive: " + stompMessage.getPayload());
runOnUiThread(() -> {
try {
resultText.append(jsonObject.getString("response") + "\n");
nameput.setText(jsonObject.getString("response"));
} catch (JSONException e) {
e.printStackTrace();
}
});
});
sendButton.setOnClickListener(v -> {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", nameText.getText());
} catch (JSONException e) {
e.printStackTrace();
}
stompClient.send(new StompMessage(
// Stomp command
StompCommand.SEND,
// Stomp Headers, Send Headers with STOMP
// the first header is required, and the other can be customized by ourselves
Arrays.asList(
new StompHeader(StompHeader.DESTINATION, Const.broadcast),
new StompHeader("authorization", "this is a token generated by your code!")
),
// Stomp payload
jsonObject.toString())
).subscribe();
nameText.setText("");
});
groupButton.setOnClickListener(v -> {
Intent intent = new Intent();
intent.setClass(BroadcastActivity.this, GroupActivity.class);
startActivity(intent);
this.finish();
});
chatButton.setOnClickListener(v -> {
Intent intent = new Intent();
intent.setClass(BroadcastActivity.this, ChatActivity.class);
startActivity(intent);
this.finish();
});
}
}
My Code in Android Studio for the Const activity:
public class Const {
public static final String TAG = "xlui";
public static final String placeholder = "placeholder";
/**
* <code>im</code> in address is the endpoint configured in server.
* If you are using AVD provided by Android Studio, you should uncomment the upper address.
* If you are using Genymotion, nothing else to do.
* If you are using your own phone, just change the server address and port.
*/
private static final String address = "ws://10.0.2.2:8080/im/websocket";//for android vm
//public static final String address = "ws://10.0.3.2:8080/im/websocket";//for Genymotion
public static final String broadcast = "/broadcast";
public static final String broadcastResponse = "/b";
// replace {#code placeholder} with group id
public static final String group = "/group/" + placeholder;
public static final String groupResponse = "/g/" + placeholder;
public static final String chat = "/chat";
// replace {#code placeholder} with user id
public static final String chatResponse = "/user/" + placeholder + "/msg";
}
My Code in Android Studio for the StompUtils activity:
import android.util.Log;
import app.xlui.example.im.conf.Const;
import ua.naiksoftware.stomp.StompClient;
import static app.xlui.example.im.conf.Const.TAG;
public class StompUtils {
#SuppressWarnings({"ResultOfMethodCallIgnored", "CheckResult"})
public static void lifecycle(StompClient stompClient) {
stompClient.lifecycle().subscribe(lifecycleEvent -> {
switch (lifecycleEvent.getType()) {
case OPENED:
Log.d(TAG, "Stomp connection opened");
break;
case ERROR:
Log.e(TAG, "Error", lifecycleEvent.getException());
break;
case CLOSED:
Log.d(TAG, "Stomp connection closed");
break;
}
});
}
}
I do not know what I did wrong, I you have any suggestions please do say.

Load .ppt file using Google docs viewer Android

I am trying to read ppt files on my Android app.But every time I run the app it crashes at startActivity().
package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri pptUri = Uri.parse("/home/waheed/check.ppt");
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("open ppt")
.setType("application/vnd.ms-powerpoint")
.setStream(pptUri )
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(shareIntent);
}
}
This is how I implemented it:
Uri fileURI = FileProvider.getUriForFile(context, "com.test.myapp", fileFromServer);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(fileURI, mimetype);
intent.setPackage(packageName);
For package name I have these possible values, depending on the extension of the file:
com.dwgsee.dwgviewer
cn.wps.moffice_eng
I must mention that I had some problems with some apps on some versions on my Android, I use 4.2.2 and 4.4.4 Android versions (my tablets have those versions), some apps for DWG opened without a problem on 4.2.2 but not on 4.4.4

PDF file not opening using Android:OnClick

So I've managed to get a button to open a pdf file using an intent, however when i run the app and press the button it says "Cannot display PDF". Can anyone help me out as to where exactly the code goes wrong?
Here is the my activity:
package com.example.android.practiceapp;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import java.io.File;
public class promotions extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promotions);
final Button button = (Button) findViewById(R.id.earlybird);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/pictures/Early_Bird_Bonus_Rules");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent1 = Intent.createChooser(intent, "Open With");
try {
startActivity(intent1);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
}
});
}
}

Android no Activity Found to Handle Intent MediaScanner

im trying to open a Gallery with Images/Videos from a specific Folder. I´m using this solution but im getting the error code below and nothing happens. I guess its something abot the Uri but i cant find a solution. Has anyone an Idea how to solve this? I also included "my" code.
03-15 16:30:53.733 21902-22775/de.comidos.fotoapp D/onScanCompleted: Scan completed: content://media/external/images/media/1730
03-15 16:30:53.752 21902-22775/de.comidos.fotoapp D/Instrumentation: checkStartActivityResult() : Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/1730 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
03-15 16:30:53.773 21902-22775/de.comidos.fotoapp W/Binder: Binder call failed.
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/1730 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1839)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1531)
at android.app.Activity.startActivityForResult(Activity.java:4389)
at android.app.Activity.startActivityForResult(Activity.java:4348)
at android.app.Activity.startActivity(Activity.java:4672)
at android.app.Activity.startActivity(Activity.java:4640)
at de.comidos.fotoapp.GalleryViewActivity.onScanCompleted(GalleryViewActivity.java:59)
at android.media.MediaScannerConnection$1.scanCompleted(MediaScannerConnection.java:55)
at android.media.IMediaScannerListener$Stub.onTransact(IMediaScannerListener.java:60)
at android.os.Binder.execTransact(Binder.java:573)
package de.comidos.fotoapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
public class GalleryViewActivity extends Activity implements MediaScannerConnection.MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";
private MediaScannerConnection conn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/comidos/sent/");
allFiles = folder.list();
// uriAllFiles= new Uri[allFiles.length];
for(int i=0;i<allFiles.length;i++)
{
Log.d("all file path"+i, allFiles[i]+allFiles.length);
}
// Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
SCAN_PATH= Environment.getExternalStorageDirectory().toString()+"/comidos/sent/"+allFiles[0];
Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
}
private void startScan()
{
Log.d("Connected","success"+conn);
if(conn!=null)
{
conn.disconnect();
}
conn = new MediaScannerConnection(this,this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
Log.d("onMediaScannerConnected","success"+conn);
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
#Override
public void onScanCompleted(String path, Uri uri) {
try {
Log.d("onScanCompleted","Scan completed: "+uri );
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally
{
conn.disconnect();
conn = null;
}
}
#Override
public void onResume(){
super.onResume();
startScan();
}
}
There is no activity on your device that supports ACTION_VIEW of a content Uri for whatever MIME type that content is. There is no requirement for an Android device to have an ACTION_VIEW activity for every possible piece of content.

How do I get the NMEA data from a serial GPS connected to my android device?

I am working on a project where I have to connect my android device to a GPS with usb. I can't figure out how I can get the data out of my GPS and I do not want to use the device it's internall GPS because I need to be pretty accurate. I have read the tutorial on the android website and I have tried to work with it but I still do not understand how I have to make connection to the GPS.
Here is my code.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.HashMap;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
UsbDevice device = deviceList.get("deviceName");
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)){
if(device != null){
//roep hier de method aan om communicatie met he apparaat te maken
}
}
else{
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
}
}
You might want to give this a shot: http://code.google.com/p/usb-serial-for-android/. Captures all serial data, does not require rooting. You would need to use Service. Let us know if you need more help. I have done this before, not on Android though.

Categories