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.
Related
I know that when I define a Broadcast receiver from manifest in bellow of intent filter I can define category that it's optional.
<receiver android:name=".PushMessageReceiver" >
<intent-filter>
<!-- Receives the actual messages. -->
<category android:name="com.test.myAppname" />
<action android:name="com.test.client.MSGRECEIVE" />
</intent-filter>
</receiver>
I googled but cant get it what is the exact point of adding category or how I can use it. I appreciate to give me some examples.
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* send
*
*/
public class MainActivity extends Activity {
private static final String MY_ACTION = "com.chaowen.action.MY_ACTION";
private Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent();
intent.setAction(MY_ACTION);
//Intent message
intent.putExtra("msg", "ha ha");
//send
sendBroadcast(intent);
}
});
}
}
MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* receive
*
*/
public class MyReceive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//get Intent message
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="send..."
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chaowen"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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>
<receiver
android:name="MyReceive"
>
<intent-filter>
<action
android:name="com.chaowen.action.MY_ACTION" />
</intent-filter>
</receiver>
</application>
I'm new to android development. At the moment I'm trying to write an app widget. I've done a few widget tutorials and when I go to run them the app will run but the widget isn't showing up on the widget list to be used. Widgets that are not extensions of an app still will run as if they are an app and will not show up in the widgets list. This is happening with all the widgets iv tried to run. No errors are showing. Somebody please help its driving me mad. Thanks
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.somethinginspiring"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.somethinginspiring.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>
<!-- declaring widget -->
<receiver android:name="AppWidgetProv"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<intent-filter >
<action
android:name="android.appwidget.action.APPWIGET_UPDATE"/>
</intent-filter>
<!-- declaring the widget_info -->
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info"/>
</receiver>
<!-- declaring broadcaster -->
<receiver
android:name="WidgetIntentReceiver"
android:label="WidgetBroadcastReceiver" >
<intent-filter>
<action android:name="android.appwidget.intent.action.CHANGE_PICTURE"/>
</intent-filter>
<!-- declaring meta-data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info"/>
</receiver>
</application>
</manifest>
widget info xml file
<?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="72dp"
android:minWidth="300dp"
android:widgetCategory="home_screen"
android:minResizeHeight="72dp"
android:minResizeWidth="300dp"
android:updatePeriodMillis="2000000">
<!-- android:updatePerMillis="3000000" -->
</appwidget-provider>
widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/widget_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/image_view_text"
android:clickable="true"
android:src="#drawable/j_1" >
</ImageView>
</LinearLayout>
app widget provider
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.widget.RemoteViews;
public class AppWidgetProv extends AppWidgetProvider {
#Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteView.setOnClickPendingIntent(R.id.widget_image_view, buildButtonPendingIntent(context));
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public PendingIntent buildButtonPendingIntent(Context context) {
Intent changePicture = new Intent();
changePicture.setAction("android.widget.intent.action.CHANGE_PICTURE");
return PendingIntent.getBroadcast(context, 0, changePicture, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteView){
ComponentName widget = new ComponentName(context, AppWidgetProv.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(widget, remoteView);
}
}
widget intent broadcast receiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class WidgetIntentReceiver extends BroadcastReceiver {
private static int currentImage = 0;
int[] images = {R.drawable.j_1};
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.appwidget.intent.action.CHANGE_PICTURE"));
RemoteViews remote = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remote.setImageViewResource(R.id.widget_image_view, getImageToSet());
}
private int getImageToSet(){
currentImage++;
return currentImage = currentImage % images.length ;
}
}
There is a typo in your manifest. This:
<action android:name="android.appwidget.action.APPWIGET_UPDATE"/>
should be changed to this:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
Notice the missing "D" in "APPWIGET_UPDATE" in the original.
The function doWork maybe run 20s, I think the system will cause ANR problem.
I don't know what kind of technology I need use to prevent ANR, AyncTask, local server, thread, IntentService or others.
Could you give me some sample codes? Thanks!
AlarmManagerBroadcastReceiver.java
package com.code4reference.enabledisablebroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
doWork(context,intent);
}
private void doWork(Context context, Intent intent){
//It will running a long time.
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.enabledisablebroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver"
android:label="#string/title_activity_enable_disable_boradcast_receiver" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Broadcast receiver -->
<receiver android:name="com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
EnableDisableBroadcastReceiver.java
package com.code4reference.enabledisablebroadcastreceiver;
import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.enabledisablebroadcastreceiver.R;
public class EnableDisableBroadcastReceiver extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnExit=(Button)findViewById(R.id.btnExit);
btnExit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public void enableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
}
public void disableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
}
}
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">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_medium"
android:text="#string/enable_broadcast_receiver"
android:onClick="enableBroadcastReceiver"
tools:context=".EnableDisableBroadcastReceiver" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_medium"
android:text="#string/disable_broadcast_receiver"
android:onClick="disableBroadcastReceiver"
tools:context=".EnableDisableBroadcastReceiver" />
<Button
android:id="#+id/btnExit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>
I'm working on android widget that displays the level of battery left.
It seem to work on the emulator except that the emulator's battery level doesn't move, so I can't test further. On real device (ICS), it only displays the initial textview("TEST") and doesn't do anything else.
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.battery"
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" >
<receiver android:name="main.MyBatteryWidget"
android:label="#string/app_name" >
<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>
<service android:name="main.MyBatteryWidget$BatteryUpdateService"
android:exported="false" >
<intent-filter>
<action android:name="com.example.battery.action.UPDATE" />
</intent-filter>
</service>
</application>
widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="0"
android:minWidth="40dip"
android:minHeight="72dip"
android:initialLayout="#layout/widget_layout" >
</appwidget-provider>
widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/textView"
android:text="TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MyBatteryWidget.java
package main;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import com.example.battery.R;
public class MyBatteryWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("TEST", "onUpdate");
context.getApplicationContext().startService(new Intent("com.example.battery.action.UPDATE"));
}
public static class BatteryUpdateService extends Service {
private static int level = 0;
private static int scale = 0;
private BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
Log.d("TEST", "receive");
updateViews(context);
}
}
};
private void updateViews(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.textView, String.valueOf(level / scale));
ComponentName componentName = new ComponentName(context, MyBatteryWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(componentName, views);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("TEST", "start");
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
}
Logcat
03-27 19:51:34.963: D/dalvikvm(608): Not late-enabling CheckJNI (already on)
03-27 19:51:35.043: E/Trace(608): error opening trace file: No such file or directory (2)
03-27 19:51:35.083: D/TEST(608): onUpdate
03-27 19:51:35.103: D/TEST(608): start
03-27 19:51:35.133: D/TEST(608): receive
For a working example have a look at my Mini Status Widget, see code
Intent.ACTION_BATTERY_CHANGED
You have to listen it in Android Manifest
<receiver android:name=".myReceiver" >
<intent-filter>
<action android:name="Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
I have just started experimenting on android. I am reading book Beginning Android 4 application development by Wrox pulication. There is a code for showing notifications. The problem is the code I have written(not copied) is very little modified. So, I am able to show notification on status/notification bar but on clicking the notification the notificaion class is not being invoked. Here is the code for android_mainfest first.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="legacy_systems.notificationproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="legacy_systems.notificationproject.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="Notification"
android:label="Details of the Notification" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Now, the code for MainActivity
package legacy_systems.notificationproject;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
int notificationID = 1;
#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.activity_main, menu);
return true;
}
public void onClick(View V)
{
getNotification();
}
protected void getNotification()
{
Intent i = new Intent(this, Notification.class);
i.putExtra("notificationID", notificationID);
PendingIntent pn = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher,
"Reminder! Meeting starts in 5 Minute",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with customer in next 2 minute";
n.setLatestEventInfo(this, from, message, pn);
n.vibrate = new long[]{ 100,250,50,500};
nm.notify(notificationID, n);
}
}
And, the code for Notification.java is
package legacy_systems.notificationproject;
import android.app.Activity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.util.Log;
public class Notification extends Activity{
String tag;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(tag,"In here");
setContentView(R.layout.notification);
NotificationManager nm = (NotificationManager)getSystemService(VIBRATOR_SERVICE);
nm.cancel(getIntent().getExtras().getInt("notificationID"));
}
}
And activity_main.xml is,
<xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/btn"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/getnot"
android:onClick="onClick"
/>
</RelativeLayout>
You would be well-advised to not name your own classes the same name as system-defined classes. You are attempting to open android.app.Notification as an activity, which will not work and should be resulting in warnings in LogCat.
Please rename your Notification activity to something else, such as Easdluaerdf, so as to make it unique, adjusting your manifest and PendingIntent to match, and you should have better luck.