Monday, July 27, 2015

Most Used Intents in Android

These intents are called Explicit Intents in Android, because you make a call that indicates exactly which activity class to work with. Remember these as Explicit Activity Calls.

Launch Another Activity

 Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);


Pass value to another activity via Intent putExtra


pass value:


Intent intent = new Intent(getApplicationContext(), NextActivity.class);
intent.putExtra("user_id", 10);
startActivity(intent);


receive value onCreate of NextActivity:


String user_id = "";
Bundle extras = getIntent().getExtras();

if(extras != null) {
    user_id = extras.getString("user_id", null);
}


Open Dialer with Phone Intent

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phone.getText().toString()));
startActivity(intent);

Open Google Maps Intent from address:

 String address = tvaddress.getText().toString(); // Get addressaddress = address.replace(" ", "+");
Intent geoIntent = new Intent (android.content.Intent.ACTION_VIEW, Uri.parse ("geo:0,0?q=" + address)); // Prepare intentstartActivity(geoIntent); // Initiate lookup

Open Google Maps Intent from lat long


String uri = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lng);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);

or

String uri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + youraddress + ")";

or

String uri = "http://maps.google.com/maps?q=geo:0,0?q=" + youraddress


Open Google Maps Intent given address string

String uri = "http://maps.google.co.in/maps?q=" + youraddress;


Open Email Programs, Specify email address and / or subejct:


Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:")); // only email apps should handle thisemailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"info@semicolondev.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback to Semicolon Developers");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hey,\n\n");
startActivity(Intent.createChooser(emailIntent, "Send Feedback..."));


Share Dialog Intent:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is your text to share");
startActivity(Intent.createChooser(intent, "Share with"));


Open Website URL

String url = "http://semicolondev.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);



Tuesday, July 21, 2015

Launcher Activity without UI login / logout case


Redirect user to different activities based on login logout stats or other intro activities on first run of the app. Launcher activity without UI helps achieve this with no UI glitches.

SET:  android:theme="@android:style/Theme.NoDisplay"
In your manifest file

        <activity
            android:name=".LauncherActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoDisplay">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

Use Activity Like this

package com.semicolondev.app;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class LauncherActivity extends Activity {

SharedPreferences preferences;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

preferences = getSharedPreferences("MyApp", 0);


String firstRun = preferences.getString("first_run", "yes");
String isLogin = preferences.getString("login", "no");

if (firstRun.equals("no") && isLogin.equals("no")){

finish();
startActivity(new Intent(getApplicationContext(), LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));

} else if ( firstRun.equals("no") && isLogin.equals("yes")){

finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));

} else {

finish();
startActivity(new Intent(getApplicationContext(), IntroActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));

}



//setContentView(R.layout.activity_main);


}




}
 

© 2013 Echo "Semicolon Developers"; Kathmandu. All rights resevered @ Semicolon Developers Network Pvt. Ltd.. Designed by Templateism

Back To Top