I have constants set up for my Flavor names:
public static final String FLAVOR_NAME_PRO = "pro";
public static final String FLAVOR_NAME_FREE = "free";
Inside my Activity's onCreate() I assign the boolean mUsingProFlavor a value:
mUsingProFlavor = BuildConfig.FLAVOR.equals(FLAVOR_NAME_PRO);
In a clickListener created later on I have this:
if (mUsingProFlavor) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
This is the only way for the user to access CustomerProfileActivity, and yet somehow I'm getting crash reports indicating that one of my Free users is crashing inside of CustomerProfileActivity.
Any idea how this could happen?
if (mUsingProFlavor) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
Your -> if(mUsingProFlavor) is the issue here.
Since mUsingProFlavor is a String, you have to modify your if-check. Currently it is acting as boolean.
if (mUsingProFlavor.equals("pro")) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
Related
I am building an android dialer application, but when my input contains string of form *123# the number which gets dialed is *123.
How can I pass # also in the dialer application?
Below is the code:
public void onDial(View v) {
if (input.getText().length() <3) {
Toast.makeText(this, "Please Enter the Valid Number", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
String hash = input.getText().toString();
if (hash.contains("#")) {
hash.replace("#", "%23");
}
intent.setData(Uri.parse("tel:" + hash));
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}else{
requestCallPermission();
}
}
}
You where close! Encoding will work, but the replace method returns a new string! :-)
if (hash.contains("#")) {
hash = hash.replace("#", "%23"); // Need to set the hash reference to the new string generated by replace()!
}
intent.setData(Uri.parse("tel:" + hash));
Refs:
http://zetcode.com/kotlin/strings/
How to use Uri.parse() with # at the end
After starting default calculator and hitting back navigation, the user is returned to a different activity rather than the calling activity. When the user clicks the calculator button, the devices default calculator opens. Upon invoking onBackPressed, the user is returned to a different activity within the app that they should not be going to. How can I make sure that the user is returned to the calling activity? Here's my code and what I have tried:
public static void openByName(Context context, String name){
ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();
final PackageManager pm = context.getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
for (PackageInfo pi : packs) {
if( pi.packageName.toLowerCase().contains(name)){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("appName", pi.applicationInfo.loadLabel(pm));
map.put("packageName", pi.packageName);
items.add(map);
}
}
if(items.size()>=1){
String packageName = (String) items.get(0).get("packageName");
Log.d(TAG, "PackageName: " + packageName);
Intent i = pm.getLaunchIntentForPackage(packageName);
if (i != null)
context.startActivity(i);
}
else{
// Application not found
Toaster.make(context, name + " not found");
}
}
After different method attempts, I had to use this way because it otherwise wouldn't work on some devices.
I would like to launch com.google.android.feedback.FeedbackActivity for my application. Like it happens in Hangouts application.
Does anyone knows which extras I need to pass to do so?
So it seems that this is possible, bur report is not visible in Developer console.
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
protected Intent prepareIcsFeedbackIntent(Activity activity, PackageManager packageManager) {
ApplicationErrorReport localApplicationErrorReport = new ApplicationErrorReport();
localApplicationErrorReport.packageName = activity.getPackageName();
localApplicationErrorReport.type = 11;
localApplicationErrorReport.installerPackageName = packageManager.getInstallerPackageName(
localApplicationErrorReport.packageName);
return getAppErrortIntent().putExtra(Intent.EXTRA_BUG_REPORT, localApplicationErrorReport);
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
protected Intent getAppErrortIntent() {
Intent localIntent = new Intent(Intent.ACTION_APP_ERROR)
.addCategory(Intent.CATEGORY_DEFAULT)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return localIntent;
}
Although it's not exactly the same, you can programmatically invoke a crash-report-dialog:
ApplicationErrorReport report = new ApplicationErrorReport();
report.packageName = report.processName = getApplication()
.getPackageName();
report.time = System.currentTimeMillis();
report.type = ApplicationErrorReport.TYPE_CRASH;
report.systemApp = false;
ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
crash.exceptionClassName = e.getClass().getSimpleName();
crash.exceptionMessage = e.getMessage();
StringWriter writer = new StringWriter();
PrintWriter printer = new PrintWriter(writer);
e.printStackTrace(printer);
crash.stackTrace = writer.toString();
StackTraceElement stack = e.getStackTrace()[0];
crash.throwClassName = stack.getClassName();
crash.throwFileName = stack.getFileName();
crash.throwLineNumber = stack.getLineNumber();
crash.throwMethodName = stack.getMethodName();
report.crashInfo = crash;
Intent intent = new Intent(Intent.ACTION_APP_ERROR);
intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
startActivity(intent);
More information here: http://blog.tomtasche.at/2012/10/use-built-in-feedback-mechanism-on.html
Just re-create that layout on a .xml file and create a Class that extends FragmentActivity (as the Google Hangouts App seems to do) or create a Class that extends DialogFragment to handle its logic.
I'm trying to send 2 parameter from one activity to another,but for some reason only one of the parameter is passed. what could it be?
Sending Activity:
showActivity(OrderActivity.class, new Pair("CUSTOMER_ID", customerId), new Pair("CUSTOMER_TYPE", customerType));
Receiving Activity : The customer id is showing 0 but the type data is got
Bundle extras = getIntent().getExtras();
long customerId = extras.getLong("CUSTOMER_ID");
int customerType = extras.getInt("CUSTOMER_TYPE");
Log.d("===customer ", " id : " + customerId + " type : " + customerType);
the showActivity looks like this
protected void showActivity(Class<? extends BaseKaizenActivity> clazz, Pair<String, Object> ... parameters) {
Intent intent = new Intent(this, clazz);
if (parameters != null) {
for (Pair<String, Object> pair : parameters) {
if (pair.second instanceof Integer) {
intent.putExtra(pair.first, (Integer)pair.second);
}
else if (pair.second instanceof Parcelable) {
intent.putExtra(pair.first, (Parcelable)pair.second);
}
}
}
startActivity(intent);
}
if (pair.second instanceof Integer) {
intent.putExtra(pair.first, (Integer)pair.second);
}
else if (pair.second instanceof Parcelable) {
intent.putExtra(pair.first, (Parcelable)pair.second);
}
Seems like you fogot to write a clause for Long.It gose neither of if nor else if,so has not been put into extradata.
why are u using that? just use put and get method of intent its very simple.eg.
intent.putExtra("backPressed", true);
getIntent().getBooleanExtra("backPressed", false);
this is my working code and got no problem.
I want to implement a search in my Android application.
In this page, first I am displaying a user list and then performing a search on the user list. Both are in the same activity. In the following manner, I am getting intent and some values from the previous page. When I display the user list, all the values are coming. But while performing search, spaceId gets lost and becomes null. I need this value.
Intent intent = this.getIntent();
Bundle receiveBundle = intent.getExtras();
spaceId = receiveBundle.getString("spaceId");
What should I do to get this value?
Edit:
String spaceId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
Bundle receiveBundle = intent.getExtras();
spaceId = receiveBundle.getString("spaceId");
String URLTicketList = String.format(getString(R.string.URLTicketList, spaceId));
RestClient client = new RestClient(URLTicketList,
this.getApplicationContext());
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
TabSettings ts = new TabSettings();
ts.setSelTab(0);
String response = client.getResponse();
tickets = parseTicketList(response);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchResult=getMatchingStrings(tickets,query);
this.ticket_adapter = new TicketAdapter(this,
R.layout.ticket_details_row,searchResult);
setListAdapter(this.ticket_adapter);
}
else {
this.ticket_adapter = new TicketAdapter(this,
R.layout.ticket_details_row, tickets);
setListAdapter(this.ticket_adapter);
}
}
getMatchingstrings()
ArrayList<Ticket> getMatchingStrings(ArrayList<Ticket> list, String regex1) {
ArrayList <Ticket> listClone = new ArrayList<Ticket>();
for (Ticket string : list) {
if(string.getAssignedTo().equals(regex1)){
listClone.add(string);
}
}
return listClone;
}
Try it
getIntent().getExtras().getString("spaceId");
You said your activity is recreating. So you are not getting the value of spaceID after recreation. Then do this thing as below.
public String PREFS_NAME = "Shared_Pref";
Bundle receiveBundle = this.getIntent().getExtras();
String spaceId_value = receiveBundle.getString("spaceId");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if(spaceId_value == null) {
spaceId_value = settings.getString("spaceId", "");
}
else {
SharedPreferences.Editor editor = settings.edit();
editor.putString("spaceId", spaceId_value);
}
I hope this will help you out.
edit :
I am editing your code.
Which part i have edited, i have mentioned that part as edited. Carefully see what i have changed and do that thing in your code. Then let me know.
String spaceId;
public String PREFS_NAME = "Shared_Pref"; //edited part
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//editing start
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Bundle receiveBundle = this.getIntent().getExtras();
if(receiveBundle != null) {
spaceId = receiveBundle.getString("spaceId");
if(spaceId == null) {
spaceId = settings.getString("spaceId", "");
}
else {
SharedPreferences.Editor editor = settings.edit();
editor.putString("spaceId", spaceId);
}
}
//editing end
String URLTicketList = String.format(getString(R.string.URLTicketList, spaceId));
RestClient client = new RestClient(URLTicketList,
this.getApplicationContext());
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
TabSettings ts = new TabSettings();
ts.setSelTab(0);
String response = client.getResponse();
tickets = parseTicketList(response);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchResult=getMatchingStrings(tickets,query);
this.ticket_adapter = new TicketAdapter(this,
R.layout.ticket_details_row,searchResult);
setListAdapter(this.ticket_adapter);
}
else {
this.ticket_adapter = new TicketAdapter(this,
R.layout.ticket_details_row, tickets);
setListAdapter(this.ticket_adapter);
}
}
When you are firstly loading your list in activity,you have to save that spaceId in some SharedPreference variable for further use.And while reloading activity with search result,you can find that Id from SharedPreference variable previously saved.
Be sure,you overwrite that variable as and when you firstly load list in your activity to meet your needs cleanly.
Also you have to get spaceId from intent like:
int spaceId=0;
if(getIntent().hasExtra())
spaceId=getIntent().getStringExtra("spaceId");
Doing this wont give you excpetion when you reload the same activity with no extras to your intent.
Or,you will have to pass this spaceId along with intent you are using to reload the same activity for showing search result.
Hope,you get the point!
If u relaunching the same activity while performing search,u will lost that value. If u r doing so,while relaunching also pass that spaceId to the relaunching activity also using intent.putextra() method
as in your question and comments i guess you are getting problem if your activity is recreated
add following code in manifest file under your activity tag
android:configChanges="keyboardHidden|orientation"
your activity will not be created again
and then use the value of spaceID where you need