Error: cannot find symbol method startActivity(Intent) - android

I need to create a simple app without activity that just open a webpage in the browser.
This is my code:
package com.example.johnny.myapplication3;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.content.Intent;
import android.net.Uri;
/**
* Testing Fundamentals
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
}
But I get this error: Error: cannot find symbol method startActivity(Intent)
Why ?

ApplicationTestCase does not have a method startActivity. If you're creating an app, not a test case, it does not make sense to use TestCase classes at all.
Without some more info on what you want to do it's hard to help more.
"a simple app without activity that just open a webpage in the browser"
How is your app supposed to be started? If it doesn't have any activity it will not have any launcher icon. Is it supposed to start by reacting to a broadcast intent or something like that? If so, you need a receiver to handle that intent and perform the actions you need to perform. You cannot just create an app with no entry point at all, how is the system supposed to know when and how to launch it?
Assuming you do want an icon in the launcher, then you do need to register an activity. That activity could in turn finish itself and launch the browser using startActivity, but the activity must be there to act as an entry point.

Related

Android start activity of second app having no icon from first app

I am having two apps. First app has an activity from which I want to launch an activity from the second app. I am using the following code:
Intent launchIntent = m_context.getPackageManager().getLaunchIntentForPackage(m_packageName);
if (launchIntent != null) {
m_context.startActivity(launchIntent);
}
This code is working very fine to launch the activity from the second app but I want to have the second application without any icon. I am using following code in MainActivity of the second application to remove icon:
PackageManager p = getPackageManager();
//Removing app icon
ComponentName componentName = new ComponentName(this, com.tools.html2pdf.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This code successfully removes the launcher icon but then activity from my first application is unable to launch the activity from second app.
Can any one help me in this regard? I want to launch activity of an app having no icon from activity of another application.
When you disable the component like you have done, that component can't be launched in any way. However, interesting thing is that other components (non-disabled activities) of your second application are still launchable.
So, you can create an alias of your MainActivity in the second application which will be used for your purpose. Let's call alias as MainActivityAlias.
From your first application, call the intent on MainActivity. The code for disabling the component will be executed and nothing will open. However, the icon will be gone because this component is disabled and everything related to this component (i.e icon) is gone too.
Now, call the intent on MainActivityAlias just after above intent in the first application. This is just a copy of MainActivity but it does not have any code for disabling and thus, it is enabled and launchable.
Some Side Notes :
1) Both activities should have an <intent-filter> with android.intent.action.MAIN.
2) Your MainActivity should be the launcher activity and thus should have android.intent.category.LAUNCHER in the manifest.
3) Inside MainActivity, you have to check where the call is coming from. If the call is from the first application, then execute the code to disable icon which you mentioned in the question. If the call is coming from launcher icon, then open MainActivityAlias using intent. You can know where the call is coming from like this.
Note - This is just an idea. I have not tested it.
If you don't want the second app to have an app icon, just remove the <intent-filter> with ACTION=MAIN and CATEGORY=LAUNCHER for the root Activity in the second app. When the app is installed, if there is no <intent-filter> with ACTION=MAIN and CATEGORY=LAUNCHER, there will be no app icon shown.
Your app can still launch the second app, but not with the method you've described, since Android doesn't know which is the "launch" Activity. Assuming you know the package and class name of the Activity you want to launch in the second app, you can launch it like this:
Intent launchIntent = new Intent();
launchIntent.setClassName("second.package.name", "fully.qualified.class.name.of.MainActivity");
// add and Intent flags if necessary here
launchIntent.addFlags(Intent.FLAG_ACTIVITY_...);
startActivity(launchIntent);

How to enqueue music to default android music player via app?

I am new to android app development, but I have good knowledge of programming in JAVA and in general. I am trying to write an app for android which can enqueue music files to default music player in android (like Google Play Music). My app decides which song to play when, but I don't want to write a full-blown music player app. I just want to feed the existing player app with new music.
I am looking for something like "inter-app" communication (perhaps using Intent?) through which I can feed content to the music player and probably control the player itself.
I am not sure if such facility exist in android, so other alternative suggestions are also welcome. Also, please let me know if I didn't explain the problem properly.
Intents are a very powerful feature of Android to which there isn't any direct analog in Java. What you want to use is a mechanism known as an implicit Intent. Normally, when you launch one activity from another, you create an Intent and specify which activity to start. With an implicit Intent, you provide an action (Intent.ACTION_VIEW) and data (a URI pointing to a music file). Using an implicit Intent, you have a piece of data handled without knowing in advance which Activity will do the handling.
When you pass your Intent to startActivity(), the OS will attempt to resolve the data in the best way possible, typically popping up a list of apps that can possibly handle your data. The user selects the appropriate app and that app handles the Intent, playing your music file. Any app that registers as a service capable of potentially handling your data will show up in the list. After passing the Intent, your activity will go into the background, and the app handling the intent will come to the foreground.
Once the user has selected an app to handle the Intent from your Activity, that app will always be used to handle that kind of Intent by your Activity until you delete your own app's data.
Take a look at the official doc to get yourself started and then ask a new question when you have a more specific problem you're trying to address.
Here's a code sample that demonstrates a very simple implicit Intent example, by which a URL is opened without knowing which browser will open it:
package com.marsatomic.intentimplicitdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
{
public final static Uri URI = Uri.parse("http://www.marsatomic.com");
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonConnect = (Button)findViewById(R.id.button_connect);
buttonConnect.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW, URI);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Could not find class when application is loaded into emulator

I have an activity (DemoAppActivity) from which I am trying to launch a different activity (MainActivity) when a button is pressed. However I am getting two errors:
1) when the app is loaded into the emulator, I see the following in the Android logs:
10-12 18:48:19.579: ERROR/dalvikvm(620): Could not find class 'com.example.android.hcgallery.MainActivity', referenced from method com.consultknapp.demoapp.DemoAppActivity$1.onClick
2) when i actually push the button that calls startActivity:
10-12 18:54:58.019: ERROR/AndroidRuntime(620): java.lang.NoClassDefFoundError: com.example.android.hcgallery.MainActivity
Here is how I am starting the activity (note: I import the class with an import statment, import com.example.android.hcgallery.MainActivity)
startActivity(new Intent(DemoAppActivity.this, MainActivity.class));
I have the MainActivity project folder in my build path in eclipse, and I even see it load the MainActivity.apk when I compile/run my DemoAppActivity. However, it bombs when I try to run it on the emulator.
What am I missing here? Do I need to jar up my MainActivity and include it in the DemoAppActivity somehow?
I was able to figure it out from another post. Basically you need to do this with the Intent before starting the activity:
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.android.hcgallery", "com.example.android.hcgallery.MainActivity"));
startActivity(i);
The setComponent tells the class loader to make it available. After adding that code, it worked!

How do I start an Activity?

Suppose that, I have an android app that launches browser with some url supplied when it receives a message containing some prespecified data(some code or something)
For this to work, my class inherits broadcastReceiver class(to receive messages).
Now as soon as it recieves a msg, it needs to launch another activity i.e browser and for this the same class needs to inherit Activity class also. But it is not possible, a class can not inherit 2 other classes.
My problem looks roughly like,
import android.content.broadCastReceiver;
import android.app.Activity;
public class sms extends broadCastReceiver{
onReceive(){
....
....
here it needs to launch another activity
}
}
Could anyone suggest how I can implement this...?
I tried creating an instance of Activity subclass inside and invoking startActivity method, but it did not work
The method for starting an activity is aContext.startActivity(new Intent(aContext, MyNewActivity.class)); Be sure that you place the proper declarations in the manifest though.
Look here.
For clarification, you can start an activity using a context. So just keep a short term reference to one and you should be fine.
EDIT:
You need to have a reference of a usable context to even create an activity. Then you do the following (using the passed reference!)
Intent i = new Intent(passedContext, MyNewActivity.class);
i.setFlags(Context.FLAG_ACTIVITY_NEW_TASK);
passedContext.startActivity(i);
Here i can launch a new activity using,
Intent i = new Intent(Context, MyNewActivity.class);
i.setFlags(Context.FLAG_ACTIVITY_NEW_TASK);
Context.startActivity(i);
here, context was passed to onReceive method of broadCastReceiver as a parameter, i can use that context itself. I can not use getBaseContext method here.

How to accept an incoming call by clicking a button?

I'm trying to implement my own phone call handling UI.
What I want to do is, if a call comes in, the incoming telephone number and a picture are displayed, and, if I press a button, the incoming call will be accepted/answered.
The related code is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
answerButton = (Button) findViewById(R.id.pickup);
answerButton.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
Intent intent = new Intent("android.intent.action.ANSWER");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Sadly, the code does not work. At first, an exception is thrown if I press my answer button:
ActivityNotFoundException: No Activity found to handle Intent {
act=android.intent.action.ANSWER
Then I added an entry in the AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
I run the app again, there is no exception anymore. However, I doubt the incoming call is not really accepted. Because if the press the Android's screen answer button (green button), the incoming call is accepted and a green in call icon is also displayed on the upper left corner of the emulator screen, while my app doesn't.
I also read the Phone app's source code in android source. There is method such as acceptCall() in the Phone class. But these codes seem difficult for me to use, because there are many imports declaration in the code, such as :
import com.android.internal.telephony.Call;
import com.android.internal.telephony.CallStateException;
import com.android.internal.telephony.CallerInfo;
import com.android.internal.telephony.CallerInfoAsyncQuery;
import com.android.internal.telephony.Connection;
import com.android.internal.telephony.MmiCode;
import com.android.internal.telephony.Phone;
And, if I add these imports in my code, there will be too many errors, such as :
The import com.android.internal.telephony cannot be resolved.
What is the right and simple way for my problem?
Add the category "android.intent.category.DEFAULT" (Intent.CATEGORY_DEFAULT)
The intent android.intent.action.ANSWER is somehow not working as expected. There is a workaround by emulating the bluetooth button to answer the incoming call. You can see an example from auto-answer project.
You need to create a broadcast receiver in which you will get the event when your phone is ringing and after that you can launch your desired activity.You can not replace the default incoming call screen until using CUSTOM ROM.
And do not forget to set the priority in broadcast receiver in manifest file.
Once you get the event you can use the object of ITelephony by using reflection.And that can provide you methods to answering or rejecting the call.
This is possible using the com.android.internal.telephony package, but you have to find someway for using this methods in eclipse and your app has to be compiled as a system app using the android source code.
Change your accept call method by this:
public static void acceptCall(Context context)
{
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}

Categories

Resources