Catch ACTION_MEDIA_BUTTON - Intent from BT-Headset - Android Software Development

Hi Community,
I have problems with the ACTION_MEDIA_BUTTON - Intent. I can't get my App receiving those actions and react on them.
In my AndroidManifest.xml I added the Intent-filter:
Code:
<intent-filter android:priority="42">
<action android:name="android.intent.action.MEDIA_BUTTON">
</intent-filter>
And in the onCreate-Method I registered a Receiver:
Code:
BroadcastReceiver receiver;
if (receiver == null) {
receiver = new ButtonReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_BUTTON);
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(receiver, filter);
And the corresponding ButtonReceiver-class:
Code:
class ButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))
{
Log.d("ButtonReceiver", "I will handle buttons here");
//handle buttons
}
}
}
The app compiles fine and is also running without problems, BUT I never get to the "//handle buttons"-section.
For testing purpose I'm using my Jabra BT2015 and the provided earphones from HTC. Both don't work.
Every hint is very appreciated.
Greez
LordAlien

Related

[Q] Item separator in ListView fails! Why?

i have a problem with ListView, i don't view line separator for each item, why ? see where is mouse pointer:
h_t_t_p://tinyurl.com/2vqg52n
and this is source code:
Java Class:
Code:
public class ScienzeInfoNewsActivity extends Activity {
private class Link {
private String title;
private String href;
public Link(String title, String href) {
this.title = title;
this.href = href;
}
public String toString() {
return title;
}
public String getHref() {
return href;
}
}
private FeedReader feedReader;
private ArrayAdapter<Link> adapter;
private ListView list;
public void initialize() {
adapter = new ArrayAdapter<Link>(this, R.layout.textview);
list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
loadFeeds(adapter);
setAllListener();
}
private void setAllListener() {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
String link = adapter.getItem((int) id).getHref();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(intent);
}
});
Button exit = (Button) findViewById(R.id.Button02);
exit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ScienzeInfoNewsActivity.this.finish();
}
});
Button refresh = (Button) findViewById(R.id.Button01);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
loadFeeds(adapter);
}
});
}
private void loadFeeds(ArrayAdapter<Link> adapter) {
if (adapter != null)
adapter.clear();
try {
feedReader = new FeedReader(new URL(
"rss.php"));
String feeds[][] = feedReader.getFeeds();
for (int i = 0; i < feeds.length; i++) {
adapter.add(new Link(feeds[i][0], feeds[i][1]));
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
R.layout.textview:
Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="h_t_t_p://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
I load entry item with this function loadFeeds()
If i select one of the items without a separator, it just one item.
Thanks for any reply.

[Q] Handling ACTION_SCREEN in a widget

Hi,
I'm doing a widget that needs to be update on screen_on or on
user_present. As I'm not hable to register a BroadcastReceiver inside
the widget I'm doing it in a Service that is triggered by the widget
like this:
Widget.java:
Code:
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.startService(new Intent(context,
WidgetService.class).setAction(WidgetService.ACTION_WIDGET_START));
}
WidgetService.java:
Code:
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
String action = intent.getAction();
Log.d(LOG_APP,"WidgetService onStart: "+action);
if (ACTION_WIDGET_STOP.equals(action)) {
this.unregisterReceiver(mReceiver);
stopSelf();
return;
} else if (ACTION_WIDGET_START.equals(action)) {
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
this.registerReceiver(mReceiver, filter);
return;
}
The problem is that when I try to register the receiver I get the
error that the service has leaked IntentReceiver because it was
already registered. Ive checked and no duplicate registration was done
in my code.
Do you have any idea what I'm overlooking?
Thanks,
PMD

[SOLVED] Lockscreen Widget forgets onUpdate init code after reopening the lockscreen

I made a simple widget, which has a few buttons. In order to make those buttons work, I use RemoteViews.setOnClickPendingIntent in the onUpdate method of the AppWidgetProvider. Works fine, as long as I place the widget only on the homescreen. But when using the widget on the lockscreen, I have the following problem:
Placing the widget on the lockscreen and immediately clicking the buttons works perfectly. But after closing the lockscreen and reopening it, the buttons don't work anymore. It seems like the widget had "forgotten" everything from the init stuff in the onUpdate method.
So, does somebody know how to fix this?
Some code snippets:
Code:
public class WidgetProvider extends AppWidgetProvider {
private static final String BUTTON_ALL_OFF = "buttonAllOff";
private RemoteViews remoteViews;
private ComponentName watchWidget;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.buttonAllOff, getPendingSelfIntent(context, BUTTON_ALL_OFF));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetProvider.class);
if (BUTTON_ALL_OFF.equals(action)) {
Log.d("test", "clicked");
//do fancy stuff
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="700dp"
android:minWidth="300dp"
android:resizeMode="vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>
Fixed it by adding the clicklistener also in the onReceive method

CountDownTimer problem

Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.

TabbedActivity (FragmentActivity): IllegalStateException: No Host, cannot reproduce

Hey All,
I've fixed quite some bugs, annoyances and crashes in our Android App and so far it looks good but a new crash has appeared which didn't occur before (probably because in the past it crashed before it even reached that point).
I tried to reproduce this crash on multiple different phones (Nexus 5, Nexus 4, Xiaomi Mi4C, Samsung Galaxy S5, HTC One A9 and A8) by rotating, switching tabs, turning screen off/on and switching tabs and rotating and so on but I just couldn't reproduce it .
Stacktrace:
Code:
java.lang.IllegalStateException: No host
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1194)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1207)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.attachFragment(SourceFile:1385)
at android.support.v4.app.BackStackRecord.run(SourceFile:728)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(SourceFile:545)
at myPackageName.FragmentTabsActivity$TabManager.onTabChanged(SourceFile:183)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:468)
at android.widget.TabHost.setCurrentTab(TabHost.java:447)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:170)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:550)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
FragmentTabsActivity manifest entry:
Code:
<activity
android:name="myPackageName.FragmentTabsActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
onTabChanged implementation from FragmentTabsActivity (I left a bit more than what is probably needed):
Code:
public static class TabManager implements TabHost.OnTabChangeListener {
private final FragmentActivity mActivity;
private final TabHost mTabHost;
private final int mContainerId;
private final Map<String, TabInfo> mTabs= new HashMap<String, TabInfo>();
TabInfo mLastTab;
static final class TabInfo {
private final String mTag;
private final Class<?> mClss;
private final Bundle mArgs;
private Fragment mFragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
mTag= tag;
mClss= clazz;
mArgs= args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext= context;
}
@Override
public View createTabContent(String tag) {
View v= new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
mActivity= activity;
mTabHost= tabHost;
mContainerId= containerId;
mTabHost.setOnTabChangedListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mActivity));
String tag= tabSpec.getTag();
TabInfo info= new TabInfo(tag, clss, args);
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
info.mFragment= mActivity.getSupportFragmentManager().findFragmentByTag(tag);
if (info.mFragment != null && !info.mFragment.isDetached()) {
FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(info.mFragment);
ft.commitAllowingStateLoss();
}
mTabs.put(tag, info);
mTabHost.addTab(tabSpec);
}
@Override
public void onTabChanged(String tabId) {
TabInfo newTab= mTabs.get(tabId);
if (mLastTab != newTab) {
FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.mFragment != null) {
ft.detach(mLastTab.mFragment);
}
}
if (newTab != null) {
if (newTab.mFragment == null) {
newTab.mFragment= Fragment.instantiate(mActivity, newTab.mClss.getName(), newTab.mArgs);
ft.add(mContainerId, newTab.mFragment, newTab.mTag);
} else {
ft.attach(newTab.mFragment);
}
}
if (newTab.mFragment != null && newTab.mFragment instanceof MyMapFragment) {
MyMapFragment mapFragment= (MyMapFragment) newTab.mFragment;
mapFragment.clearBundleOnTabChange();
}
if (mLastTab != null && mLastTab.mFragment != null && mLastTab.mFragment instanceof MyMapFragment) {
MyMapFragment mapFragment= (MyMapFragment) mLastTab.mFragment;
mapFragment.clearBundleOnTabChange();
}
mLastTab= newTab;
ft.commitAllowingStateLoss();
mActivity.getSupportFragmentManager().executePendingTransactions();
}
}
}
FragmentTabsActivity is hosting 3 tabs where as every tab has one assigned Fragment.
If anyone knows how to reproduce/solve this I would be really happy about an answer. I've already found 2 or 3 possible solutions while googling but without being able to test which one actually solves my problem I feel uncomfortable :/
If needed and if it helps I can also add and describe the lifecycle of the FragmentTabsActivity and the hosted fragments
No ideas?

Categories

Resources