Activities and the manifest in Android - android

I know there are plenty of questions about this but I just can't work out why my program keeps loading the wrong activity even though I've made the correct one the default in the manifest. Here's some code for you all:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="swin.examples"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="swin.examples.TemperatureConvertor"
android:label="#string/app_name">
</activity>
<activity android:name="swin.examples.FeetToCmConvertor"
android:label="#string/app_name">
</activity>
<activity android:name="swin.examples.Main" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here's where I think the problem will be if it's not in the manifest:
package swin.examples;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUI();
}
private void initializeUI()
{
Button btnCm = (Button)findViewById(R.id.btnFeet);
btnCm.setOnClickListener(btnFeetListener);
Button convertButton = (Button)findViewById(R.id.btnTemp);
convertButton.setOnClickListener(btnTempListener);
}
/** Handle convert button click */
private OnClickListener btnTempListener = new OnClickListener()
{
public void onClick(View v)
{
convertButtonClicked();
}
};
private OnClickListener btnFeetListener = new OnClickListener()
{
public void onClick(View v)
{
btnCmClicked();
}
};
private void btnCmClicked()
{
// set the sender and the receiver of the intent
Intent intent = new Intent();
intent.setClass(getApplicationContext(), swin.examples.FeetToCmConvertor.class);
startActivity(intent); // transmit your intent
}
private void convertButtonClicked()
{
// set the sender and the receiver of the intent
Intent intent = new Intent();
intent.setClass(getApplicationContext(), swin.examples.TemperatureConvertor.class);
startActivity(intent); // transmit your intent
}
}
I can't work it out and I hope you can help me! Thank you so much in advance!
EDIT: Code updated, but still doesn't work. If this helps, this is the log messages:
[2013-04-20 22:44:20 - CombinedConvertor] Success!
[2013-04-20 22:44:21 - CombinedConvertor] Starting activity swin.examples.TemperatureConvertor on device emulator-5554
[2013-04-20 22:44:23 - CombinedConvertor] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=swin.examples/.TemperatureConvertor }

Proper IntentFilter for the main Activity is
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Weirdest bug ever. Worked for everyone else, just not me. Remade temperatureconverter activity and it works now. Thank you everyone for your help!

Related

Launch Activity in android via Dialpad using Secret Code

I just want to launch activity via Secret Code .
Although , there are many solutions available for this problem but none of them worked for me. It seems like BroadcastReceiver is not listening .
I am using Mi4i MIUI 7.
Here's my code. Please Help!!
MainActivity.java
package com.example.jatin.myapplication2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have even tried the below commented code , but that didn't work.
MySecretCodeReceiver.java
package com.example.jatin.myapplication2;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Jatin on 05-Aug-16.
*/
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String uri = intent.getDataString();
String sep[] = uri.split("://");
if (sep[1].equalsIgnoreCase("1234"))
{
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.jatin.myapplication2.MainActivity");
context.startActivity(launchIntent);
}
else if (sep[1].equalsIgnoreCase("5678")) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("net.one97.paytm");
context.startActivity(launchIntent);
}
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// setResultData(null);
// context.startActivity(i);
}
}
}
I have also tried putting android:host="1234" in manifest ,still not getting desired results
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
<receiver android:name="receivers.MySecretCodeReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
<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>
</application>
Got it ....
We have to register our MySecretCodeReceiver as a <receiver> within the <application> element in the AndroidManifest.xml file.
And , Its Done. :)

using of intent for linking activities

I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.

Android:Broadcast demo not working

I am new to android, i am trying a broadcast demo, I gave my best effort by reading the documentation but its not working. Please have a look at my code:
BroadcastDemoActivity.java
package com.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BroadcastDemoActivity extends Activity {
/** Called when the activity is first created. */
public static final String PUBLIC_HOLIDAYS = "com.paad.action.PUBLIC_HOLIDAYS";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
}
}
Receive.java
package com.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String message = intent.getStringExtra("Holiday");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BroadcastDemoActivity"
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=".Receive">
<intent-filter>
<action android:name="com.paad.action.PUBLIC_HOLIDAYS"/>
</intent-filter>
</receiver>
</application>
</manifest>
I know that i am missing something which i am not aware of, please help.
I believe your problem is in the call to sendBroadcast.
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
You are not sending the intent that you construct, you're sending the intent returned from getIntent(), which will be the intent that the activity was started with.
It should be
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(intent);

Stopping an Activity in Android

I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".NewKFCActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpPageOne"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGEONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpPageTwo"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGETWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SelectDifficulty"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.SELECTDIFFICULTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
This is my main activity:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
startActivityForResult(intent,0);
finish();
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivityForResult(intent,0);
finish();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is my difficulty class:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class SelectDifficulty extends Activity{
ImageButton bEasy, bMedium, bHard;
#Override
protected void onCreate(Bundle kfcState) {
// TODO Auto-generated method stub
super.onCreate(kfcState);
setContentView(R.layout.difficultyxml);
bEasy = (ImageButton) findViewById(R.id.easy);
bEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
//startActivityForResult(intent,0);
}
});
}
}
First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.
Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.
Hope it helps :)
Cheers!
Remove the lines:
finish();
after
startActivityForResult(...);
in your Activity NewKFCActivity because finish() is destroying your activity and when you press back, there is no more an Activity to go back to!

Login successful but unable to open the next activity

I have login successfully without the intent. But when I add the Customer Activity intent it gives me an force closed error when trying to login. I have a listview of 5 diff. activities within the customer.java and they all open with no problem. Im wondering why the Login is giving the error
Login.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends Activity {
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
/** Called when the activity is first created. */
//#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("guest")){
lblResult.setText("Login successful.");
Intent i = new Intent("com.merrill.Customer");
startActivity(i);
} else {
lblResult.setText("Login failed. Username doesn't match.");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
// Close the application
finish();
}
});
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.royalcabinets.merrill"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Login"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.merrill.Customer"
android:label="Customer Service Group">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
See RonnieĀ“s response or
Intent i= new Intent(Login.this, Customer.class);
or
Intent i= new Intent(this, Customer.class);
or
Intent i = new Intent(getApplicationContext(), Customer.class);
and
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.royalcabinets.merrill"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Login"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Customer"
android:label="Customer Service Group">
</activity>
</application>
</manifest>
Your problem lies in having two points of entry for your app. The Manifest should have only one Activity marked with the intent-filter action for android.intent.action.MAIN.
Besides that, I still feel there can be other sources of problems in your code. I suggest you fire up the ddms tool. [go to the terminal/command prompt and execute the ddms.bat file in your android-sdk\tools folder]. Set a filter to print the Errors, and you'll find some meaningful message.
post that, and we can take it forward - that is, if removing the extra MAIN Activity doesn't work.

Categories

Resources