Android use of Service in an Activity - android

import android.app.Activity;
import android.app.Service;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
public class Preview extends Activity {
public Preview() {
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = new Intent();
intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = Service.class.getPackage().getName();
}
}
I am curious about the use of Service.class.getPackage().getName();. I see the package name is gotten. My question is on the use of Service.class. What is meant by that or what object is being accessed.

Usually, this is used to initialize classes in Java (the know pattern is Class.forName("some.Clazz") ). It forces a class to be loaded and its static fields to be initialized (eg. commonly for JDBC drivers).
Now you're under andoid (then dalvik), where did you get this pattern?

It's a roundabout to get a String containing "android.app". As a side effect, it also loads the Service class (which is completely useless since it's a framework class and doesn't have any static initializers). If you're looking for the application's package name (the one declared in the manifest), you really should do this.getPackageName() (since Activity is a Context)

A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not
directly interacting with the application). This corresponds to calls
to Context.startService(), which ask the system to schedule work for
the service, to be run until the service or someone else explicitly
stop it.
A facility for an application to expose some of its functionality to other applications. This corresponds to calls to
Context.bindService(), which allows a long-standing connection to be
made to the service in order to interact with it
Source: Android Dev Docs
Source: What is a service

Related

Launch an android application using by hardware button press

Thank you for reading my question and please don't mention the older answers because I went through every bit of old ones in here that nothing work out. And I tried to find something from Responding to media buttons in android developer documentation but I didn't understand it. Please be kind enough to support me!
I am developing an android application for blind people Therefore I want this application launch by using specific hardware button clicks. How can I do this? Any advice, help, pointers welcome!
Try to create broadcastreceiver:
package com.example.test;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log;
public class CallBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
Log.i("Tag", "Action : "+ intent.getAction() + " / volume : "+volume);
}
}
Create App class that extends Application class and register this receiver. If it wont help - you will need to create separate service that will listen to user actions.

Error: cannot find symbol method startActivity(Intent)

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.

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;
}
}

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