Android: Twitter in WebView - Back to an Activity by callback - android

I try to create Twitter client and now I deal with authorization via OAuth protocol. I have created "Sign In" button to come in WebView and load twitter authorization URL, that's work. However, when the authorization is accepted successfuly and Twitter service redirect me to my callback I receive error web page in WebView. That is to say I am not redirected to my activity, I still stay in WebView. But if try the same way via browser, it`s working. What the problem is that?
Main Activivty:
public class Twitter extends Activity implements OnClickListener {
Button bSignIn;
TextView status;
private OAuthConsumer consumer;
private OAuthProvider provider;
private String url;
final String TAG = getClass().getName();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
bSignIn = (Button) findViewById(R.id.bSignIn);
status = (TextView) findViewById(R.id.tvStatus);
bSignIn.setOnClickListener(this);
}
public void onClick(View v) {
new OAuthWebViewProcess().execute();
}
public class OAuthWebViewProcess extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(Twitter.this, null,
"Connecting, please wait...");
}
protected Void doInBackground(Void... params) {
try {
consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL,
Constants.ACCESS_URL, Constants.AUTHORIZE_URL);
url = provider.retrieveRequestToken(consumer,
Constants.OAUTH_CALLBACK_URL);
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
}
return null;
}
protected void onPostExecute(Void result) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Intent i = new Intent(Twitter.this, TwitterWebView.class);
i.putExtra("url", Uri.parse(url).toString());
startActivityForResult(i, 1);
dialog.dismiss();
}
}
}
WebView for Twitter:
public class TwitterWebView extends Activity {
String url;
WebView TwitterWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitterwebview);
Bundle extras = getIntent().getExtras();
url = extras.getString("url");
try {
TwitterWebView = (WebView) findViewById(R.id.wvTwitter);
TwitterWebView.setWebViewClient(new TwitterWebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
TwitterWebView.getSettings().setJavaScriptEnabled(true);
TwitterWebView.getSettings().setDomStorageEnabled(true);
TwitterWebView.getSettings().setSavePassword(false);
TwitterWebView.getSettings().setSaveFormData(false);
TwitterWebView.getSettings().setSupportZoom(false);
TwitterWebView.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wixanz.app.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Twitter"
android:label="#string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TwitterWebView"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<activity
android:name=".TweetList"
android:label="TweetList"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="twitter" />
</intent-filter>
</activity>
</application>
</manifest>

I did the same about others networks like LinkedIn, Foursquare. But instead of use the callback URL, I override the method shouldOverrideUrlLoading (WebView view, String url) in your WebViewClient (which is used to show the login page) to catch the access token and the token secret (if needed) by myself.

Related

I want call Unity funtion on android activity

public void CameraOnClick(){
using (AndroidJavaClass javaClass = new AndroidJavaClass ("com.d3d.viewer.NativePlugin")) {
using (AndroidJavaObject currentActivity = javaClass.CallStatic<AndroidJavaObject> ("instance")) {
Debug.Log ("Start Camera############");
//javaClass.CallStatic ("showCamera");
currentActivity.Call("showCamera");
}
}
}
public void getimageUrl1(string ImageUrl){
imageurl1 = ImageUrl;
Debug.Log ("ImageUrl11111111111111111111"+ImageUrl);
}
Android source
public class NativePlugin extends UnityPlayerActivity {
final Activity b = m_instance;
final Activity a = UnityPlayer.currentActivity;
private static NativePlugin m_instance;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UnityPlayer.UnitySendMessage("PlusPopupManager","getimageUrl1","22222"); //don't activate
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
public static NativePlugin instance()
{
if(m_instance == null)
{
m_instance = new NativePlugin();
}
return m_instance;
}
public void showCamera () // Connect Camera
{
UnityPlayer.UnitySendMessage("PlusPopupManager","getimageUrl1","111111"); //activate
a.runOnUiThread(new Runnable()
{
public void run() {
Intent intent = new Intent(a, NativePlugin.class);
a.startActivity(intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
UnityPlayer.UnitySendMessage("PlusPopupManager","getimageUrl1","333333");//don't activate
}
}
manifast.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.d3d.viewer">
<application android:allowBackup="true" android:label="#string/app_name"
android:supportsRtl="true">
<activity android:name="com.d3d.viewer.NativePlugin" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="#string/app_name" android:screenOrientation="landscape" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
</manifest>
I make ARR android plugins.and import Unity3d.
'UnityPlayer.UnitySendMessage' is activated when I call 'showCamera'.
but don't activate when I call intent( NativePlugin) funtion.
why don't call 'UnityPlayer.UnitySendMessage' funtion on android activity.

Opening android deep links from app

I am implementing deep links inside my app and could not find a way, or example about opening them from inside my own app. For example: I wish that opening certain banner would open myapp://game/1 link which would lead to another activity inside my app. How can I do that ?
In the manifest you should register the deep linking scheme.
<activity android:name=".DeepLinkingActivity"
android:configChanges="orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myapp" />
</intent-filter>
</activity>
With this the DeepLinkingActivity will open when the the link with the defined scheme is clicked. And in the activity handle what to do:
private final String GAME_LINK = "game";
private final String VIDEO_LINK = "video";
private static String PASSED_LINK = "PassedLink";
public static Intent createIntent(String link, Context context) {
Intent intent = new Intent(context, DeepLinkingActivity.class);
intent.putExtra(PASSED_LINK, link);
return intent;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String host;
String link = getIntent().getStringExtra(PASSED_LINK);
if(TextUtils.isEmpty(link)) {
Intent intent = getIntent();
if (intent.getData() != null) {
Uri data = intent.getData();
host = data.getHost();
} else {
// No links
}
} else {
Uri data = Uri.parse(link);
host = data.getHost();
}
if(host.equals(GAME_LINK)) {
// myapp://game/
// Do something
} else if(host.equals(VIDEO_LINK)){
// myapp://video/
// Do something
} else {
// Do something
}
...
}
Then you can call from your widget:
widget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(DeepLinkingActivity.createIntent("linik_for_this_wiget"), getContext());
}
});
If you have links in WebView you could also override shouldOverrideUrlLoading

Message not received when sending a message from Wear to Phone [MessageAPI]

I am trying to send a simple message from my wear [Emulator] to my android phone, The message should have been sent according to my logs on the wear but it does not trigger my "showToast" method on my phone [it should be triggered when a message is received]. Anyone has an idea what I could be doing wrong?
This is my Wear Manifest
<manifest package="georgikoemdzhiev.weartesttwo"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature android:name="android.hardware.type.watch"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.DeviceDefault">
<uses-library
android:name="com.google.android.wearable"
android:required="false"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
This is my Mobile manifest
<manifest package="georgikoemdzhiev.weartesttwo"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".ReceiveMessageService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
</intent-filter>
</service>
</application>
This is my Wear logic [I have a button that sends the showToast message]
public class MainActivity extends WearableActivity {
private static final long CONNECTION_TIME_OUT_MS = 2500;
private static final String TAG = MainActivity.class.getSimpleName();
private CircularButton mSendButton;
private List<Node> myNodes = new ArrayList<>();
private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
new SimpleDateFormat("HH:mm", Locale.UK);
private BoxInsetLayout mContainerView;
private TextView mTextView;
private TextView mClockView;
private GoogleApiClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
mSendButton = (CircularButton)findViewById(R.id.sendToast);
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendToastMessage();
}
});
mClient = new GoogleApiClient.Builder(this)
.addApiIfAvailable(Wearable.API)
.build();
getNodes();
mContainerView = (BoxInsetLayout) findViewById(R.id.container);
mTextView = (TextView) findViewById(R.id.text);
mClockView = (TextView) findViewById(R.id.clock);
}
private void sendToastMessage() {
Log.d(TAG,"Sending message... Nodes List size:" + myNodes.size());
// send toast message logic...
new Thread(new Runnable() {
#Override
public void run() {
for(Node n:myNodes) {
Log.d(TAG,"Sending message to node:"+n.getDisplayName());
Wearable.MessageApi.sendMessage(mClient,n.getId(),"/showToast",null);
}
}
});
}
private List<Node> getNodes(){
new Thread(new Runnable() {
//
#Override
public void run() {
Log.d(TAG,"Getting nodes...");
mClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mClient).await();
List<Node> nodes = result.getNodes();
for(Node n:nodes){
Log.d(TAG,"Adding Node: "+n.getDisplayName());
myNodes.add(n);
}
Log.d(TAG,"Getting nodes DONE!");
}
}).start();
return null;
}
}
This is my ReceiveMessageService in Mobile
public class ReceiveMessageService extends WearableListenerService {
#Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d("ReceiveMessageService","onMessageReceived");
//if(messageEvent.getPath().equals("/showToast")) {
showToast(messageEvent.getPath());
//}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
This is my MainActivity in Mobile
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.d(TAG,"onConnected");
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG,"onConnectionSuspended");
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.d(TAG,"onConnectionFailed");
}
It looks like you are using wrong pathPrefix in your mobile side AndroidManifest. Try to replace
<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
with
<data android:scheme="wear" android:host="*" android:pathPrefix="/showToast" />
Edit
Also keep in mind that MessageApi is not guarantee to deliver a message even if it returns a successful result code as Google's document stated:
Note: A successful result code does not guarantee delivery of the message. If your app requires data reliability, use DataItem objects or the ChannelApi class to send data between devices.

NoClassDefFoundError:with PreferenceFragmentCompat

I am doing an application using the PreferenceFragmentCompat when i call the PreferenceFragmentCompat i am getting the error like java.lang.NoClassDefFoundError i am getting the confused what to do i have tried and checked some google suggestion it is not solving the problem please help to solve it
my Class which extends PreferenceFragmentCompat is as i am showing the code below
public class AutoAnswerPreferenceActivity extends PreferenceFragmentCompat implements OnSharedPreferenceChangeListener {
private AutoAnswerNotifier mNotifier;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
mNotifier = new AutoAnswerNotifier(getActivity());
mNotifier.updateNotification();
SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
/*PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);*/
}
#Override
public void onDestroy() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("enabled")) {
mNotifier.updateNotification();
}
}
#Override
public void onCreatePreferences(Bundle arg0, String arg1) {
// TODO Auto-generated method stub
}
and my fragment class is as shown below
public class AutoAnswarFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, new AutoAnswerPreferenceActivity()).commit();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AutoAnswerPreferenceActivity ()).commit();
}
}
In Navigation Drawer i am calling the fragment to replace it as shown below
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AutoAnswarFragment(),null).commit();
the manifest file is as shown below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".NavigationDrawerMainActivity"
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="com.koteswara.wise.autoanswer.AutoAnswerReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name="com.koteswara.wise.autoanswer.AutoAnswerBootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.koteswara.wise.autoanswer.AutoAnswerIntentService" />
<activity android:name="com.koteswara.wise.settings.GetCallerInfoActivity"
android:theme="#android:style/Theme.Dialog"
></activity>
</application>
please help to solve this i will be great full to you people. I am getting this error from the last one week

callback url not returning to the android screen

Linkedin authorization callback url not returning to the android screen. Below is the code which i have tried:
String linkedinKey = "xxxxx"; //add your LinkedIn key
String linkedinSecret = "xxxx"; //add your LinkedIn Secret
public static final String OAUTH_CALLBACK_SCHEME = "callback";
public static final String OAUTH_CALLBACK_URL = "x-oauthflow-linkedin" + ":///"+ "callback";
LinkedInRequestToken LinkedinrequestToken ;
Intent i;
final LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(linkedinKey,linkedinSecret);
final LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(linkedinKey,linkedinSecret);
LinkedInApiClient client;
ImageView btnLinkedInLogin=(ImageView)findViewById(R.id.btnLinkedInLogin);
btnLinkedInLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinkedinrequestToken = oauthService.getOAuthRequestToken(OAUTH_CALLBACK_URL);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(LinkedinrequestToken.getAuthorizationUrl()));
// i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
}
});
private void OnNewIntent()
{
Log.d("newintent","hi");
String verifier = i.getData().getQueryParameter("oauth_verifier");
LinkedInAccessToken accessToken = oauthService.getOAuthAccessToken(LinkedinrequestToken, verifier);
Log.d("token",accessToken.toString());
}
Manifest.xml
<activity
android:name=".Login"
android:launchMode="singleTop"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#style/AppThemes" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="t4jsample"
android:scheme="oauth" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="x-oauth-linkedin" android:host="callback" />
</intent-filter>
</activity>
But my webpage stops at authorization successful. It doesn't return to the android screen.
My OnNewIntent() function is not called.
As I can see, You have used onNewIntent in wrong way, just use like this:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("newintent","hi");
String verifier = i.getData().getQueryParameter("oauth_verifier");
LinkedInAccessToken accessToken = oauthService.getOAuthAccessToken(LinkedinrequestToken, verifier);
Log.d("token",accessToken.toString());
}
You can invoke onNewIntent always by putting it into onCreate method like below, so make sure if you did like this:
#Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
onNewIntent(getIntent());
}
Hope this will help. Thanks. :)

Categories

Resources