Toast and Broadcast receiver doesn't work - android

Hello i tried many times when i click the button it doesn't show the toast and broadcast receiver
the code attached down there kindly correct my miss takes its run successfully but when click the button nothing happend at all , I tried to edit in activity_main.xml but nothing happend
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// broadcast a custom intent.
public void broadcastIntent(View view){
Intent intent = new Intent();
intent.setAction("com.myapplication.BOOT_COMPLETED"); sendBroadcast(intent);
}
}
MyReceiver.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.myapplication.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Broadcast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="#+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:minWidth="48dp"
android:minHeight="48dp"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton"
android:layout_centerHorizontal="true"
android:onClick="broadcastIntent"
android:text="broadcast_intent" />
</RelativeLayout>

I hope this could help
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver br;
IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
br = new MyReceiver();
filter= new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
public void broadcastIntent(View view){
Intent intent = new Intent();
intent.setAction("com.myapplication.BOOT_COMPLETED");
sendBroadcast(intent);
this.registerReceiver(br, filter);
}
}
also see this link
https://developer.android.com/guide/components/broadcasts

You are using a Manifest-declared receiver. This receiver would get invoked outside from other app or with some system events.
The receiver then becomes a separate entry point into your app which means that the system can start the app and deliver the broadcast if the app is not currently running.
If you want to trigger this receiver within the app you should register this receiver. In your case inside the MainActivity within onCreate or onResume as follows
broadcastReceiver = new MyReceiver ()
Intent filter = new IntentFilter("com.myapplication.BOOT")
registerReceiver(broadcastReceiver, filter)
Also you need to unregister it within onDestroy or onPause
unregisterReceiver(broadcastReceiver)
I guess you should use Context-registered receiver for your use case where you want to send a broadcast within the app. Its almost the same except that you don't need to define it inside the manifest.
More information here

Related

ANDROID - BroadcastReceiver and ImageView

im learning Android, and im triying to make and app that changes the image in the imageView when I rotate the phone, I´ve tried to get the broadcast receiver value, I would like some advice in how to do it, here's the code, sorry if its to much code, but i dont know where i am missing
Main Activity:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver miReceptor;
IntentFilter intentFilter;
Intent intent;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
miReceptor = new Receptor();
intentFilter = new IntentFilter("android.intent.action.CONFIGURATION_CHANGED");
}
#Override
public void onStart(){
super.onStart();
miReceptor.onReceive(context, intent);
registerReceiver(miReceptor,intentFilter);
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
tools:context="com.example.gaboc.covonesrevenge.MainActivity">
<ImageView
android:id="#+id/puerta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.814"
app:srcCompat="#drawable/puerta_cerrada01" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/covone_revenge" />
</android.support.constraint.ConstraintLayout>
BroadcastReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receptor extends BroadcastReceiver {
private final String ROTATION_CHANGED="android.intent.action.CONFIGURATION_CHANGED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ROTATION_CHANGED)){
Toast.makeText(context, "COVONE'S REVENGE", Toast.LENGTH_LONG).show();
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gaboc.covonesrevenge">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CONFIGURATION_CHANGED"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Receptor">
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED"/>
</intent-filter>
</receiver>
</application>
</manifest>

Broadcast Receiver not showing result when broadcasting from another app Activity

I am trying to make broadcast receiver which will show a toast message when user click on button from my another app activity.
But receiver not showing result.
My code is below
My Receiver App
MyReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast has been received!", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xyz.receivebroadcast">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.xyz.broadcasts"></action>
</intent-filter>
</receiver>
</application>
My sender app
My MainActivity which sends the broadcasts is here.
MainActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SendOutBroadcast(View view){
Intent i = new Intent();
i.setAction("com.example.xyz.broadcasts");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(i);
}
}
activity_main.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xyz.broadcasts.MainActivity">
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SendOutBroadcast"
android:text="Send Broadcast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
In your case your activity Intent should be like this:
Intent intent = new Intent();
intent.setAction("com.example.xyz.broadcasts");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(
new ComponentName("com.pkg.AppB","com.pkg.AppB.MainActivity"));
sendBroadcast(intent);
Try this may help you or visit for more information.
NOTE - This may be not going to work in Android O because in Android O implicit broadcast are ban as mentioned Here. (Also credits to you #Muhammad Arslan Maqsood)
i found the issue: issue is not with code, actually Android O disabled the feature "Implicit broasdcast". see this

Broadcastreceiver can be called from ADP but not other apps

I have a broadcast receiver I am using to display some notification. I can call it and have it be correctly triggered using ADB. But calling it from within another app does nothing.
The receiver does live in/on a Android Wear app/device.
Receiver
<receiver android:name=".NotificationReceiver" android:exported="true">
<intent-filter>
<action android:name="site.com.app.SHOW_NOTIFICATION" />
</intent-filter>
</receiver>
Call from ADB
./adb -s SERIAL shell am broadcast -a site.com.app.SHOW_NOTIFICATION
Call from App
Intent i = new Intent();
i.setAction("site.com.app.SHOW_NOTIFICATION");
i.putExtra("contentText", "Some Text");
sendBroadcast(i);
I'm not sure why it would work from ADB but not from another App, any ideas?
I don't know exact your code but you can try below code which I have made for you.
**Second Application**
package com.example.jiteshmohite.callingbroadcast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lunchExportedBroadcast();
}
});
}
private void lunchExportedBroadcast() {
Intent intent = new Intent();
intent.setAction("com.example.exportedreceiver");
sendBroadcast(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.jiteshmohite.callingbroadcast.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:text="Call Broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="18dp"
android:id="#+id/button" />
</RelativeLayout>
**First Application who register broadcast**
public class ExportedReceiver extends BroadcastReceiver {
public ExportedReceiver() {`enter code here`
// empty constr
}
#Override
public void onReceive(Context context, Intent intent) {
// showing toast whenever any external application call these receiver.
Toast.makeText(context.getApplicationContext(), "ExportedReceiver", Toast.LENGTH_SHORT).show();
}
}
**Register receiver in First Application**
<receiver android:name=".receiver.ExportedReceiver">
<intent-filter >
<action android:name="com.example.exportedreceiver"></action>
</intent-filter>
</receiver>

splash screen shown as blank screen

I made a splash layout, that last for a time and then other activity should be loaded,alone it works perfectly, but when it's connected to other activity, it shows blank screen.
mainifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andy.propaganda"
android:installLocation="auto" >
<application
android:allowBackup="true"
android:icon="#drawable/my_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar"
>
<activity
android:name=".shit"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Splash"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
splash activity :
package andy.propaganda;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Andrew on 2/4/2016.
*/
public class Splash extends Activity {
//welcome animation
ImageView loading_img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
loading_img = (ImageView)findViewById(R.id.loading_view);
final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim);
loading_img.setAnimation(animatable);
//
for(int i = 0;i< 100000000;i++);
Intent intent = new Intent(this, shit.class);
intent.putExtra("jhjh", 8);
startActivity(intent);
}
}
main activity :
package andy.propaganda;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Andrew on 2/5/2016.
*/
public class shit extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
splash.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/MyBlue"
>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/my_launcher"
android:paddingTop="60dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/loading"
android:layout_marginBottom="43dp"
android:id="#+id/loading_view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="loading your files"
android:id="#+id/welcome_message"
android:focusable="false"
android:textColor="#010101"
android:textSize="#dimen/abc_action_bar_progress_bar_size"
android:layout_above="#+id/loading_view"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView"
android:layout_marginBottom="50dp" />
</RelativeLayout>
You should use timer on splash screen instead of for(int i = 0;i< 100000000;i++); which will wait for some time suppose 5 sec and than load another Activity.
package andy.propaganda;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Andrew on 2/4/2016.
*/
public class Splash extends Activity {
//welcome animation
ImageView loading_img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
loading_img = (ImageView)findViewById(R.id.loading_view);
final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim);
loading_img.setAnimation(animatable);
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// your task
Intent intent = new Intent(this, shit.class);
intent.putExtra("jhjh", 8);
startActivity(intent);
}
}, 5000);
}
}
RxJava solution:
Observable.empty().subscribeOn(AndroidSchadelurs.mainThread()).delay(500, TimeUtils.MILISECONDS).subscribe(this::startActivity(this, new Intent));
First of All dont use for Loop for waiting time instead of That use Handler with something like this :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Splash.this, shit.class));
finish();
}
}, 500);
Then remove this line from your manifest.xml :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Rest of your code seems ok :)
Also take look at this link which well-explained about Splash Screen.

Do android widgets require default activity to launch?

I searched a lot for this question and tried everything mentioned on the internet but still couldn't find a solution. My widget gets installed but does not update on clicking. I have not created a default activity for it.
My problem is that my action on clicking the button is not changing.
Here is my code:
//Android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.pictureapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<receiver
android:name="MyWidgetIntentReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.android.intent.action.CHANGE_PICTURE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
</application>
</manifest>
//MyWidgetProvider.java- this class contains the onUpdate method and has listeners.
package com.example.android.pictureapp;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.example.android.pictureapp.R;
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){
//Log.i("Tag","In onUpdate method");
System.out.println("Tag In onUpdate method");
RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
Log.i("Tag","Context value after setOnClickPI before pushWIDGET:"+context);
pushWidgetUpdate(context,views);
Log.i("Tag","after pushwidget update:"+context);
}
public static PendingIntent buildButtonPendingIntent(Context context){
Intent intent=new Intent();
intent.setAction("com.example.android.intent.action.CHANGE_PICTURE");
Log.i("Tag","intent's action:"+intent);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context,RemoteViews views){
System.out.println("Inside pushwidget");
ComponentName myWidget=new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager=AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, views);
}
}
//MywidgetIntentReceiver.java - This class contains onReceive method and performs the function to do after clicking.
package com.example.android.pictureapp;
import com.example.android.pictureapp.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class MyWidgetIntentReceiver extends BroadcastReceiver {
private static int clickCount = 0;
#Override
public void onReceive(Context context, Intent intent) {
// Log.i("Tag1","In onReceive:"+intent);
System.out.println("In onReceive()");
if (intent.getAction().equals(
"com.example.android.intent.action.CHANGE_PICTURE")) {
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setImageViewResource(R.id.widget_image, getImageToSet());
System.out.println("in updateWidgetPicture method");
// Log.i("Tag","in updaeWIdgetPicture method");
// remember to set ur button click listeners
views.setOnClickPendingIntent(R.id.widget_button,
MyWidgetProvider.buildButtonPendingIntent(context));
MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
views);
}
private int getImageToSet() {
clickCount++;
Log.i("Tag", "in getImageToSet()" + clickCount);
return clickCount % 2 == 0 ? R.drawable.paypal_logo : R.drawable.paypaldonation;
}
}
//layout/widget_layout.xml
<?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="wrap_content"
android:orientation="vertical"
android:layout_margin="5sp"
>
<ImageView
android:id="#+id/widget_image"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="center"
android:src="#drawable/paypal_logo"
/>
<Button
android:id="#+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Change it"
/>
</LinearLayout>
//xml/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:minHeight="146dp"
android:updatePeriodMillis="5000"
android:initialLayout="#layout/widget_layout"
>
</appwidget-provider>
//MainACtivity
package com.example.android.pictureapp;
import com.example.android.pictureapp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
#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;
}
}
And these are the changes to the manifest:
<activity
android:name="com.example.android.pictureapp.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>
Starting Android 3.1, your app must have at least one Activity that the user can launch before it will receive any broadcasts etc.
Since you don't have an Activity, your widget's broadcast probably isn't received because the system thinks your app is in a stopped state. Try adding an Activity.

Categories

Resources