I have made a counter widget. There are two buttons, one to increase and one to decrease the number. The number is displayed on a TextView. At first everything works just fine, but after some time (usually around an hour) the Textview freezes i.e. I click the buttons but the number doesen't change.
When I click the buttons, the "clicking animation" is still played, so the buttons might still work.
When I remove the widget and replace it again, the widget starts to work again but the same problem occurs after around the same time.
The code is here:
WidgetProvider
package com.example.myapplication;
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 android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class WidgetProvider1 extends AppWidgetProvider {
String widgettextviewtext;
final String INCREASE= "sdfjkldjldkf";
final String DECREASE = "skdhbcvouweoaior";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
MainActivity.zahl = 15;
remoteViews.setTextViewText(R.id.textView, ""+MainActivity.zahl);
remoteViews.setOnClickPendingIntent(R.id.button3,onClickPendingIntent(context,ZAHLGROESSER) );
remoteViews.setOnClickPendingIntent(R.id.button2,onClickPendingIntent(context,ZAHLKLEINER) );
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
updateWidgetNow(context,remoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
#Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//if increase button is clicked
if(intent.getAction().equals(INCREASE)){
if(MainActivity.zahl == 15) {
Toast.makeText(context, "STOOOP!!!", Toast.LENGTH_SHORT).show();
}else{
MainActivity.number = MainActivity.number+1;
String text=""+ MainActivity.number;
remoteViews.setTextViewText(R.id.textView, text);
updateWidgetNow(context, remoteViews);
}
}
//if decrease button is clicked
if(intent.getAction().equals(DECREASE)) {
if (MainActivity.number == 0) {
Toast.makeText(context, "Hurray!", Toast.LENGTH_SHORT).show();
}else{
MainActivity.number = MainActivity.number - 1;
String text = "" + MainActivity.number;
remoteViews.setTextViewText(R.id.textView, text);
updateWidgetNow(context, remoteViews);
}
}
super.onReceive(context, intent);
}
public void updateWidgetNow (Context context, RemoteViews remoteViews){
ComponentName widgetComponent = new ComponentName(context, WidgetProvider1.class);
AppWidgetManager.getInstance(context).updateAppWidget(widgetComponent, remoteViews);
}
public PendingIntent onClickPendingIntent (Context context, String stringAction){
Intent onClickIntent = new Intent(context, WidgetProvider1.class);
onClickIntent.setAction(stringAction);
return PendingIntent.getBroadcast(context,0,onClickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
}
}
MainActivity:
package com.example.myapplication;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static int number = 15;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.MyApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.myapplication.WidgetProvider1"
android:label="Tutorial Widget"
android:exported="true"
>
<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>
</application>
</manifest>
WidgetInfo:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="180dp"
android:minWidth="180dp"
android:initialLayout="#layout/widget_layout"
android:previewImage="#drawable/widget_preview"
android:widgetCategory="home_screen"
android:resizeMode="horizontal|vertical"
>
</appwidget-provider>
WidgeLayout:
<?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:theme="#style/Theme.AppCompat.Light"
android:id="#+id/widget_layout"
>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_centerInParent="true"
android:text="1"
android:textAlignment="center"
android:textSize="150dp"
android:textColor="#color/white"
/>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/textView"
android:layout_marginBottom="1dp"
android:text="increase"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:textSize="30dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_marginTop="1dp"
android:text="decrease"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:textSize="30dp"
/>
</RelativeLayout>
Related
I am trying to use intents but on clicking any of the buttons,the app stops working and terminates.I am a beginner and I couldn't find the reason.I am providing my xml file,java file and menifest file.Please someone help.
here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.example.shreya.intents.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Alarm"
android:id="#+id/alarm"
android:onClick="setAlarm"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Map"
android:id="#+id/map"
android:onClick="seeMap"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mail"
android:id="#+id/mail"
android:onClick="email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email ID"
android:inputType="textCapWords"
android:id="#+id/address"/>
</LinearLayout>
And here is my .java file:
package com.example.shreya.intents;
import android.content.Intent;
import android.net.Uri;
import android.provider.AlarmClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import static com.example.shreya.intents.R.id.alarm;
public class MainActivity extends AppCompatActivity {
private Button mail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setAlarm(){
String message="Wake Up";
int hour=7;
int minutes=0;
Intent intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
public void seeMap(){
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 28.699884, 77.273075);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uri));
if(intent.resolveActivity(getPackageManager())!=null)
startActivity(intent);
}
public void email(){
EditText mail=(EditText)findViewById(R.id.address);
String add=mail.getText().toString();
composeEmail(add,"shreya");
}
public void composeEmail(String addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT,"hello");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
And finally this is my menifest file:
<?xml version="1.0" encoding="utf-8"?>
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You have to add parameter View view to your onClick methods.
For example:
public void setAlarm(View view){
}
Add that parameter to other onClick methods also: email and seeMap.
Under the hood, the system uses reflection to figure out the exact onClick method in the Activity and the system uses the following exact pattern to find the method: a public void method with the specified method name in the xml onClick attribute and has a single parameter View.
I am trying to get the ABOUT button to listen for a click, in order to go from a screen displaying three buttons and a picture, to another screen just displaying some text.
This is my Main java file (MainActivity.java)
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context cont = this;
Button aButton = (Button) findViewById(R.id.ab_button);
aButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(cont, AboutActivity.class);
startActivity(intent);
}
});
}
This is my second java file for the about button (AboutActivity.java):
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AboutActivity extends Activity {
public void onCreate(Bundle about){
super.onCreate(about);
setContentView(R.layout.activity_about);
TextView briText = (TextView) findViewById(R.id.about_b2);
briText.setText("Goodie");
}
}
This is the main xml file that displays the picture and the buttons (activity_main.xml):
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_b"
android:id="#+id/ab_button"
android:shadowColor="#8e2300"
android:background="#drawable/oval_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/share_button"
android:id="#+id/share_button"
android:shadowColor="#8e2300"
android:onClick="shareActivity"
android:background="#drawable/oval_button"
And this is the xml file that displays the text (activity_about.xml):
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/back_to_work_rel">
<TextView android:text="#string/about_bac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/about_b2" />
</RelativeLayout>
Manifest file (AndroidManifest.xml):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brian.eggalong" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity android:name=".AboutActivity"
android:parentActivityName=".MainActivity"
android:label="#string/about_bac">
</activity>
</application>
</manifest>
Every time I click the ABOUT button, nothing happens. Any help would be greatly appreciated.
Try to remove the background attribute of your button.
Otherwise you can try this
MainActivity.java
public void moveToAboutActivity(){
Intent i = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(i);
}
activity_main.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_b"
android:id="#+id/ab_button"
android:onClick="moveToAboutActivity"/>
good Luck
Ok, so, definetly the problem is that you need to put the activity in your manifest file like:
<activity
android:name=".activity.SplashScreen"
android:screenOrientation="portrait"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have a problem while writing code of bluetooth adapter in android. Bluetooth adapter is always remains null. I have written code for that but I could not turn on the bluetooth.
My Code is as below
package com.example.bluetoothdemo;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
#SuppressLint("NewApi")
public class MainActivity extends Activity {
String msg="Hello";/* Default message to be sent. */
BluetoothAdapter adpt=null;// Default it is set to null value.
public static String MacAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.button_send);
final TextView tv=(TextView)findViewById(R.id.disp_msg);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
adpt=BluetoothAdapter.getDefaultAdapter();
if(adpt==null){
// Always this portion is generating errors.
tv.append("Bluetooth Not Available in device");
Toast.makeText(getApplicationContext(), "Bluetooth is off",Toast.LENGTH_LONG).show();
}
else{
if(!adpt.isEnabled()){
//adpt.enable();
Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivity(i);
startActivityForResult(i, 0);
tv.append("Bluetooth is turned on.");
}
}
byte[] toSend=msg.getBytes();
try{
final UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothDevice device=adpt.getRemoteDevice(MacAddress);
BluetoothSocket socket=device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
socket.connect();
OutputStream mmout=socket.getOutputStream();
mmout.write(toSend);
mmout.flush();
mmout.close();
socket.close();
Toast.makeText(getBaseContext(), MacAddress, Toast.LENGTH_SHORT).show();
}catch(IOException e){
e.printStackTrace();
}
}
});
}
#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;
}
}
My activity_main.xml file is as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<TextView android:id="#+id/disp_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/display"
/>
</LinearLayout>
My AndroidMenifest.xml file is as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.bluetoothdemo.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>
</application>
</manifest>-
I have created the .apk file also and run on my Micromax A89 Ninja phone but the error says that "BluetoothDemo has been stopped unexpectedly."
Please write the below line
adpt=BluetoothAdapter.getDefaultAdapter();
in OnCreate() method after this line,
setContentView(R.layout.activity_main);
I made a project which must generate a page on entering message, but it is not working for a reason. I am posting the code below.
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/hello_world" />
<EditText
android:id="#+id/edit1"
android:layout_margin="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Write a message"
>
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter"
android:onClick="send"
/>
</LinearLayout>
MainActivity.java
package com.example.postapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText edit1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1 = (EditText) findViewById(R.id.edit1);
button1 = (Button) findViewById(R.id.button1);
}
#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;
}
public void send(View v)
{
String msg = edit1.getText().toString();
if(msg.length()>0)
{ //Toast.makeText(getBaseContext(), "enter length", Toast.LENGTH_SHORT).show();
HttpClient httpc = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://localhost/demo/server_scripr.php");
//Toast.makeText(getBaseContext(), "enter length0", Toast.LENGTH_SHORT).show();
try
{
List<BasicNameValuePair> vp = new ArrayList<BasicNameValuePair>();
//vp.add(new BasicNameValuePair("id","01"));
vp.add(new BasicNameValuePair("message",msg));
httpost.setEntity(new UrlEncodedFormEntity(vp));
httpc.execute(httpost);
edit1.setText("");
Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(getBaseContext(), "Please enter the field", Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.postapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.postapp.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>
</application>
</manifest>
Thanks, help me in finding the solution
The error is most likely within this line of your code:
HttpPost httpost = new HttpPost("http://localhost/demo/server_scripr.php");
Make sure it isn't supposed to be:
/server_script.php
I have no idea what's going on. My OnReceive method is never called. It's probably something in the manifest.
Widget Provider:
package com.net_refresh;
import android.app.PendingIntent;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.widget.RemoteViews;
import android.appwidget.AppWidgetManager;
public class NetRefreshWidget extends AppWidgetProvider
{
private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
#Override
public void onUpdate(Context context, AppWidgetManager appwidgetmanager, int[] appWidgetIds)
{
final int length = appWidgetIds.length;
for (int i=0; i<length; i++)
{
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, NetRefreshWidget.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_initial_layout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
appwidgetmanager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds)
{
}
#Override
public void onEnabled(Context context)
{
}
#Override
public void onDisabled(Context context)
{
}
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
//Top secret code here.
}
}
Manifest:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.net_refresh" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name=".NetRefresh">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".NetRefreshWidget">
<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>
</application>
</manifest>
Widget info XML:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="#layout/widget_initial_layout"
>
</appwidget-provider>
Widget layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_root"
android:layout_width="72dp"
android:orientation="vertical"
android:layout_height="72dp"
>
<Button android:text="Button" android:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="wrap_content"></Button>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/icon" android:clickable="true" android:adjustViewBounds="false" android:id="#+id/img_refresh" android:layout_toRightOf="#+id/button1" android:layout_alignTop="#+id/button1" android:layout_alignBottom="#+id/button1">
</ImageView>
</RelativeLayout>
The code says nothing without you to point the compiler's errors & warnings.
Anyway you can wrap your code into .apk and install it by yourself into your device, BUT this is the curve way, because everyone I know are using the eclipse and SDK tools.
Have you tried using this: <uses-permission android:name="android.permission.INTERNET" />. It may solve it.
I figured it out a while back. I don't remember what I changed exactly but I at least added this line:
<action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
To the manifest, next to:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
Very simple solution, now that I have an understanding of how broadcasts work.