have an android widget which is actually a button to start an activity.The widget works fine using this code but there is a small bug :
When I add the widget to the home screen , clicking it first time doesn't do anything , second time it works. Then it works normally from 1st click. And the same thing happens when the phone is restarted.Can someone help me solve this problem ??
MyWidgetIntentReceiver.java
public class MyWidgetIntentReceiver extends BroadcastReceiver {
@override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("Start")) {
onUpdate(context);
}
}
private void onUpdate(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_demo);
Intent configIntent = new Intent(context, WidgetFlash.class);
configIntent.putExtra("widget", 1);
configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent configPendingIntent = PendingIntent.getActivity(context,
0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_button,
configPendingIntent);
MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
remoteViews);
} }
MyWidgetProvider.java :
public class MyWidgetProvider extends AppWidgetProvider {
@override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);
remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("Start");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}}
Widget in Manifest:
<receiver android:name="com.hybernect.flashlight.MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/demo_widget_provider" />
</receiver>
<receiver
android:name="com.hybernect.flashlight.MyWidgetIntentReceiver"
android:label="widgetBroadcastReceiver" >
<intent-filter>
<action android:name="Start" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/demo_widget_provider" />
</receiver>
The logcat shows nothing when I click first time. Please help !
Related
Hello,
As my application is running as a service, it uses the BOOT_COMPLETED and USER_PRESENT events to start.
I tested it on different devices running Android 2.x but today I realised it doesn't work on a Google Nexus 3 which runs Icecream Sandwich.
Both booting and unlocking events does not start the application.
I use the following code :
Code:
<receiver
android:enabled="true"
android:name="myapp.StartReceiver">
<intent-filter>
<action android:name = "android.intent.action.BOOT_COMPLETED"/>
<action android:name = "android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
...
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
// ...
Intent service = new Intent(context, MyService.class);
context.startService(service);
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
// ...
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
So I wonder if it is related to the Android version.
Any idea why it does not work on this specific phone ?
Thank you
Read this.
I guess it is why it doesn't work. Thank you.
I've got a ViewPager and a TextView inside it. When content of TextView is larger then it is visible on screen, there shall be possibility to scroll it vertically. But it does not do this automatically
Here is an xml for TextView
Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/txText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#338877"
android:padding="15dp"
android:scrollHorizontally="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
And here is an xml for ViewPager
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vpPonkSwiper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/lbPonkFooter" />
<TextView
android:id="@+id/lbPonkFooter"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Footer" />
</RelativeLayout>
Here is an Adapter:
Code:
public class PonkPageAdapter extends PagerAdapter
{
private int pagesCount = -1;
private List<Ponk> data;
private App app;
private MockPersistence db;
private Activity activity;
private TextView currentView;
private LayoutInflater inflater;
public PonkPageAdapter(Activity activity, byte section) {
this.activity = activity;
this.app = ((App) activity.getApplication());
this.db = app.getPersistence();
this.section = section;
this.data = db.findPonksBySection(section); // new LinkedList<Ponk>();
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//
this.pagesCount = db.countPonksInSection(section);
}
@Override
public int getCount()
{
return pagesCount;
}
@Override
public Object instantiateItem(View collection, int position)
{
TextView tv = getNewView();
Ponk j = data.get(position);
tv.setText(j.text);
tv.setTag(j.id);
setFontSizeInSP(tv, app.getFontSize());
tv.setTag(position);
((ViewPager) collection).addView(tv, 0);
return tv;
}
private TextView getNewView()
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = (TextView)inflater.inflate(R.layout.Ponk, null);
return tv;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
currentView = (TextView) object;
}
@Override
public void destroyItem(View collection, int position, Object view)
{
((ViewPager) collection).removeView((TextView) view);
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == (TextView) object;
}
@Override
public Parcelable saveState()
{
return null;
}
}
Hello, I am trying to create a broadcast receiver to detect when the screen was locked. I have it already working to detect when the the tablet was booted, but it won't work for the SCREEN_OFF action. Right now I just have it log any intents that where triggered and so far its only working for when the boot was completed.
Code:
package com.atlantis;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class broadcastReceiver extends BroadcastReceiver {
[user=439709]@override[/user]
public void onReceive( Context context , Intent intent ) {
Log.v( "atlantis" , "Hey this intent was triggered " + intent );
if ( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()) ) {
Intent launchMain = new Intent ( context, MainActivityService.class );
context.startService( launchMain );
}
}
}
Android manifest:
Code:
<receiver
android:name = "com.atlantis.broadcastReciever"
android:enabled = "true"
android:exported = "false" >
<intent-filter>
<action android:name = "android.intent.action.ACTION_SCREEN_OFF" />
<action android:name = "android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Thanks
reply
did you write anything for screen lock intent... if yes display that code..
Hi, i'm try to create a simple clock that needs to be updated every minute, I want to use a BroadcastReceiver that listen the ACTION_TIME_TICK, however, it never launches, I have the action registered in the AndroidManifest:
Code:
<receiver android:name=".MainWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider">
</meta-data>
</receiver>
<receiver android:name=".WidgetBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.TIME_TICK"></action>
</intent-filter>
</receiver>
The class MainWidget extends from AppWidgetProvider:
Code:
[user=439709]@override[/user]
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);
updateWidget(context);
context.startService(new Intent(context, WidgetBroadcastReceiver.class));
}
public void updateWidget(Context context){
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.activity_main_widget);
updateViews.setTextViewText(R.id.test, actualDate);
ComponentName myComponentName = new ComponentName(context, MainWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
}
And the class WidgetBroadcastReceiver from BroadcastReceiver where I have this on the onReceive method:
Code:
if(action.equals(Intent.ACTION_TIME_TICK)){
Log.i("TICK", "WORKS");
}
But it never adds anything in the log, any help? Thanks in advanced, have a nice day!
I cant listen for key event in `BroadcastReceiver`
I want to do something on KEYCODE_VOLUME_DOWN or KEYCODE_VOLUME_UP but in fact it is do nothing.
So here is app code
Code:
public class MainActivity extends Activity {
private MyReciver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyReciver();
}
@Override
public void onResume() {
IntentFilter filter = new IntentFilter(Intent.EXTRA_KEY_EVENT);
registerReceiver(myReceiver, filter);
super.onResume();
}
private class MyReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
KeyEvent event = null;
if (Intent.EXTRA_KEY_EVENT.equals(intentAction)) {
event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
}
if (event == null) {
return;
}
int keycode = event.getKeyCode();
int action = event.getAction();
if (keycode == KeyEvent.KEYCODE_VOLUME_DOWN|| keycode == KeyEvent.KEYCODE_VOLUME_UP) {
if (action == KeyEvent.ACTION_DOWN) {
Log.e("KeyEvent", "Trigerd");
if (isOrderedBroadcast()) {
abortBroadcast();
}
}
}
};
}
}
And here is code from Manifest
Code:
<receiver
android:name="MyReciver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.EXTRA_KEY_EVENT" />
</intent-filter>
</receiver>
Can anyone explain my how to do what I want right?