Dear developers,
After searching the whole internet and stackoverflow I didn't find the answer to my problem, so I need your help.
Problem
I'm trying to make an app that monitors your headset-jack-plug. To code below shows that i register the broadcast receiver and the Logcat output confirms that. But as soon as I test this app on my HTC Desire Bravo (connected to Eclipse), the onreceive method is never called.
When I replace the action.HEADSET_PLUG by action.ACTION_POWER_DISCONNECTED, the code DOES work. So when I unplug my powercable and plug it in again, my Logcat shows me the JackPlugReceiver_debug_tag.
Code
(urls replaced by #### )
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="####"
package="com.wassenaar.antitheft"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name="com.wassenaar.antitheft.MainActivity"
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="JackPlugReceiver" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
Code:
package com.wassenaar.antitheft;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class JackPlugReceiver extends BroadcastReceiver {
[user=439709]@override[/user]
public void onReceive(Context context, Intent intent) {
Log.d("JackPlugReceiver_debug_tag", "onReceive method of JackPlugReceiver has run!");
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d("JackPlugReceiver_debug_tag", "Headset is unplugged");
break;
case 1:
Log.d("JackPlugReceiver_debug_tag", "Headset is plugged");
break;
default:
Log.d("JackPlugReceiver_debug_tag", "I have no idea what the headset state is");
}
}
}
}
Logcat
Code:
D/PackageManager(96): Receivers: com.wassenaar.antitheft.JackPlugReceiver
I hope you guys can help me out, I don't know what to do anymore.. Thanks!
Maybe you could share your BroadcastReceiver class and where you register it.
hgpb said:
Maybe you could share your BroadcastReceiver class and where you register it.
Click to expand...
Click to collapse
Stupid, forgot to post that! See the edit above. This is all the code I got.
From what I read, this intent is sent out FLAG_RECEIVER_REGISTERED_ONLY. You won't be able to define the intent in XML
http://stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-into-an-android-device-or-not/6366238#6366238
Thanks for making this clear. Thought I already tried registering in run time, but apparently I did something wrong. Nevertheless, I learned something new. Thanks for the helpful link to stackoverflow!
Problem solved!
Related
I have decided to recreate a game I was making on the PC for Google Android but I have run into some snags. Currently I have the menu partially created but am not exactly sure how the views and such work. So I have uploaded my source and was hoping someone could look at my source and help me. I have menu created through xml and have ninepatchdrawable button with a background set for the menu. What I am trying to figure out is clicking start and going to another view.
http://www.bobhoil.com/wp-content/uploads/2010/08/T-Dodger.zip
You can also visit my blog here: http://bobhoil.com/
Thanks! I hope I can get some assistance
I am also having some trouble installing it to my att Samsung Captivate. I tried using a program I have been using to install from the pc but it says wrong SDk version even though it is the same one I have been using.
Please Help?
look at this: http://dl.dropbox.com/u/2817943/T Dodger.rar
i have added an additional layout and an acitivity-class to your project. and i called that activity by clicking you start button!
i hope that helps you
Thanks that helps a ton! That was the main issue I was having. Now I can start connecting parts of my game together. I appreciate it a ton.
Also just a tip, root your captivated and turn on USB debugging. That will allow you to run your program right to your phone, rather than the slow emulator.
Sent from my HTC HD2 using XDA App
Okay that sounds good. One thing though that voids your warrenty and I bought an extended warrenty from At&t. So would it void my warrenty?
Also I am making some progress. I have the code laid out for detecting the accelerometer! Next I just need to use that data and apply it to the image that I will be moving around the screen.
It will not void warrenty as it is software based. Sounds good. Hope to see a beta soon.
Okay sounds great then. I will start working on that then.
I have run into another little problem. Now I have rooted my phone which makes testing a LOT easier! (Thanks Metastable!)
The following code seems to cause it to crash:
package com.bobhoil.tdodger;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
public class TestActivity extends Activity implements SensorEventListener {
private TextView accelXValue;
private TextView accelYValue;
private TextView accelZValue;
private SensorManager sensorManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.test);
accelXValue = (TextView) findViewById(R.id.accel_x_value);
accelYValue = (TextView) findViewById(R.id.accel_y_value);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
accelXValue.setText("0.00");
accelYValue.setText("0.00");
accelZValue.setText("0.00");
}
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelXValue.setText(Float.toString(sensorEvent.values[0]));
accelYValue.setText(Float.toString(sensorEvent.values[1]));
accelZValue.setText(Float.toString(sensorEvent.values[2])); }
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
sensorManager.unregisterListener(this);
super.onStop();
}
}
Click to expand...
Click to collapse
This is supposed to capture movement through the accelerometer.
please zip the eclipse project and tell us the link to it.
i'm too lazy to build my own xml with 3 EditText's
OKay here is the link: http://www.bobhoil.com/wp-content/uploads/2010/09/T-Dodger2.zip
I have been busy working and hadn't had a chance to upload it.
Don't mean to be persistent I am just trying to figure out why it is crashing. I changed the code in the above file to some ibm examples just to test if that worked and it too seems to crash. Am I starting the activity wrong?
there are some attributes missing in your test.xml (android:layout_width... and so on)
here is the correct test.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mobil"/>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accelerometer_label"
android:layout_column="1"
android:text="Accelerometer"
android:textSize="9pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_x_label"
android:layout_column="1"
android:text="X:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_x_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_y_label"
android:layout_column="1"
android:text="Y:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_y_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_z_label"
android:layout_column="1"
android:text="Z:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_z_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip" android:layout_width="fill_parent"
android:background="#FF909090" />
</LinearLayout>
by replacing the test.xml with the correct one, the activity opens like it should (you can check that by adding a breakpoint in the onCreate method).
But there is another error too that will cause the activity to close immediately. At the moment i have no idea what's the problem, but i try to figure it out!
Okay thanks. I have been trying to solve the problem for about a week now. I am having a hard time figuring out the problem.
EDIT:
Okay after doing some research maybe it is getting information from the accelerometer to quickly?
no, the problem is the test.xml again... something is wrong with your table-rows.
while executing your application, logcat shows some exceptions like "null-child" in a table row...
Code:
09-05 07:45:40.114: ERROR/AndroidRuntime(223): java.lang.NullPointerException
09-05 07:45:40.114: ERROR/AndroidRuntime(223): at android.widget.TableRow.measureNullChild(TableRow.java:182)
I'm afraid I have too little experience with <TableRow> in Android. I suggest to use simple <TextView> elements to display the sensor data. You only need one TextView, and in your java class file you can put all the sensor values in this TextView.
for example:
accelValue.setText("Accel X: " + values[0]+"\n" + "Accel Y: " + values[] +.....)
The TestActivity is for testing purposes only... so don't care about the layout
Okay I will change over to using TextView and see then if I can get something up and running.
Thanks for the help! I have finally got it fully working!
OKay one last question. Is there any way to change android:layout_y outside of the xml? From the activity?
Have you set up your activities in manifest.xml file??
Sent from my ADR6300 using XDA App
I'm just trying to receive a simple SEND broadcast.
THanks
the class that wants to receive this intent has to extend BroadcastReceiver or one of its children. you then override the onReceive() method like this
Code:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals("MY_SEND") {
//do something
}
}
and of course on the senders end you can always do the setAction() method on the intent you are sending. setAction("MY_SEND");
Thank you, everything that I have found is some over-engineered solution.
Its always a challenge to find the piece that you need from an example/tutorial. Most of the times simpler is better
Glad i could help
From something awesome
If my Receiver class was "Receiver.java" should my androidmanifest look like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vlvgroup.TestSend"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestSend"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<receiver android:name=".receiver" android:enabled="true">
<intent-filter>
<action android:name="com.vlvgroup.TestSend.receiver" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Should work. Are you trying to recieve a broadcast from a class inside your package or from outside?
I may be wrong but the intents defined in the AndroidManifest.xml are so outside programs can send broadcasts to you. While internal private broadcasts dont need to be defined, such as one from an AlarmManager being used to do periodic work.
From something awesome
basically I want to use the "Share Page" from the browser and sent it to my app.
You mind sharing how to get an app to be in that list when you figure it out? Im not sure how its done and im interested
From something awesome
killersnowman said:
You mind sharing how to get an app to be in that list when you figure it out? Im not sure how its done and im interested
From something awesome
Click to expand...
Click to collapse
Code:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
Adding that to the activity intent filters.
So how would you distinguish between the share page in the browser and share in lets say the photo app? Is it by the mimeType? Or would your app show up wherever there is a share?
From something awesome
Good question, I would I assume mimetype
I'm trying to make a keyboard and I've looked at the SDK sample code, which uses InputMethodService. But if I use that, it renders a little phone-sized keyboard on the screen (I'm using an Iconia tab running honeycomb) and I want to use the whole screen's width.
Oddly, even getMaxWidth() returns 1280 or 800, so it does seem aware the display is bigger but it's not filling it.
The following simple code should reproduce what I'm seeing:
Code:
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
public class MyKeyboard extends InputMethodService {
@Override
public boolean onEvaluateFullscreenMode() {
return true;
}
}
Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blah.android.softkeyboard">
<application android:label="@string/ime_name">
<service android:name=".MyKeyboard"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method" />
</service>
</application>
</manifest>
I've just discovered if I disable compatibility mode in Spare Parts, it now works full screen. I assume I'm missing something trivial?
edit---
okay fixed. I was missing android:minSdkVersion
[Solved] I didn't see a place to mark the problem solved, so I did it here so no one would waste their time. Thanks.
I'm trying to get my app to respond anytime a new package is installed.
I have registered the intent in the manifest and created the receiver class, but I can't get onReceive to fire.
While I was playing around with it and trying to debug, I added a constructor to my receiver, and the constructor fires every time that a package is installed. The onReceive method never fires with the constructor in or out though.
I'm not sure where I am going wrong. Anyone have any ideas on what I am doing wrong? Thanks.
Code:
<receiver android:name=".InstallReceiver">
<intent-filter >
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Code:
public class InstallReceiver extends BroadcastReceiver {
public InstallReceiver()
{
//This log will display in the logcat
Log.d("InstallReceiver", "InstallReceiver constructor called.");
}
[user=439709]@override[/user]
public void onReceive(Context context, Intent intent) {
//This log never displays if the constructor is in or commented out
Log.d("InstallReceiver", "Install detected.");
}
}
Why do you have <category android:name="android.intent.category.DEFAULT" /> in your receiver definition? I assume your app isn't just a broadcast receiver??
It was one of the suggested fixes I found in the hours I've spent searching the internet for answers. It still has the same problem if you remove it though. I guess I just forgot to take that one out before I pasted my code.
I created a new project, and cleared all the data from my phone before uninstalling and then re-installing, and it magically started working. So, I'm not sure what the issue was.
I love the mysterious fixes!
I am trying to combine 2 android apps through a button. The first app is just a regular screen that has one button on it. The second app is an app launcher that shows all apps and opens them when clicked on. I want to make it so that when i click the button in the first app it opens up the second app. I have looked at questions similiar to mine on Stackexchange and have taken their advice by making the second app into a library and importing it, but when i click the button the app shuts down. Am i missing something?
This is the manifest file for the first app:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/symbol"
android:label="@string/app_name">
<activity
android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar"
android:launchMode="singleTask"
android:name="com.Fuku.Eiko.MainActivity"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<activity
android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar"
android:launchMode="singleTask"
android:name="com.Assistant.Eiko.AppDrawer"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
This is my xml for button:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/symbol_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
This is the java file for the first app:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import com.assistant.Eiko.*;
import android.content.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */ @override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(com.assistant.Eiko.R.id.button);
button.setOnClickListener
(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, com.assistant.Eiko.AppDrawer.class);
startActivity(intent);
}
});
}
I have asked this question on StackExchange but no answers yet. Thank you in advance for any help given. Please be as descriptive as possible. Also I am not using eclipse i am using AIDE( android app editor).
Sent from my GT-P3113 using XDA Premium 4 mobile app