Notification class not loading - android

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.

Related

How to create a notification at a specific time in the future? (Android)

Right now I want to choose a specific time and send a notification to the user, but the app only sends a notification when running in the background. How can I send a notification when the application is completely closed?
This is my MainActivity.java class
package com.vortex.notification;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE,48);
calendar.set(Calendar.SECOND,3);
Intent intent = new Intent(getApplicationContext(),Notification_reciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
});
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "LemubitReminderChanel";
String description = "Chanel for Lemubit Reminder";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("notifyLemubit", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
This is my Notification_reciver.java class
package com.vortex.notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
public class Notification_reciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repating_intent = new Intent(context,Repating_activity.class);
repating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"notifyLemubit")
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Bildirim Başlığı")
.setContentText("Bildirim Yazısı")
.setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
And this my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vortex.notification">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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>
<receiver android:name=".Notification_reciver" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</receiver>
</application>
</manifest>
I had same issue and I solved it by changing this line
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
To this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,0);
And in manifest change your receiver from this
<receiver android:name=".Notification_reciver" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</receiver>
To this:
<receiver android:name=".Notification_reciver"
android:enabled="true"
android:exported="true"
android:process=":remote" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</receiver>

Getting Parse Push Notification in List view

I am using parse push notification for my app , the notification comes fine but on clicking the notification I need to show them in a list view , I searched for tutorials but I could not find any . Please help me . Please explain with code , I am new to android . Thanks in advance.
here is my custom receiver.
public class CustomReciever extends BroadcastReceiver {
NotificationCompat.Builder mBuilder;
Intent resultIntent;
int mNotificationId = 001;
Uri notifySound;
String alert;
#Override
public void onReceive(Context context, Intent intent) {
try{
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
alert = json.getString("alert").toString();
}catch (JSONException e){
}
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(alert));
mBuilder.setPriority(Notification.PRIORITY_HIGH);
mBuilder.setSmallIcon(R.drawable.syncytium);
mBuilder.setContentText(alert);
mBuilder.setContentTitle("Syncytium");
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setAutoCancel(true);
resultIntent = new Intent(context, com.omc.sunny.syncytium.syncytium.Notification.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId,mBuilder.build());
}
}
here is my Notification class
public class Notification extends AppCompatActivity {
TextView notifTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
notifTv = (TextView)findViewById(R.id.notif);
}
#Override
protected void onNewIntent(Intent intent) {
String message = getIntent().getExtras().getString("alert");
notifTv.setText(message);
}
}
At first add this to your manifest
-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="YOURPACKAJE.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="YOURPACKAJE.permission.C2D_MESSAGE" />
add it to application tag
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="YOURPACKAJE.notifications.MyReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="YOURPACKAJE" />
</intent-filter>
</receiver>
then create class in packaje YOURPACKAJE.notifications.MyReceiver
import java.util.List;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.TextView;
import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;
public class MyReceiver extends ParsePushBroadcastReceiver {
protected void onPushReceive(Context mContext, Intent intent) {
Log.e("ParsePush", "RECIVED");
if (intent.hasExtra("com.parse.Data")){
String jsonString=intent.getExtras().getString("com.parse.Data");
Log.e("", "json " + jsonString);
JSONObject json = new JSONObject(jsonString);
String title= json.getString("title");
String message= json.getString("message");
// then call your method to create manually your custom notification with pending intent
//in intent putExtra("title",title), putExtra("message",message)
//and the after opening in Activity catch this intent
}
}
}
in wersite parse.com send notification like JSON
{"title":"your tittle is here","message":"your message"}
dont use getIntent() , use intent in method:
#Override
protected void onNewIntent(Intent intent) {
String message = intent.getExtras().getString("alert");
notifTv.setText(message);
}

Autorun app when boot completed

I need to start an app when the [Android] phone starts.
I compiled this code, and the app doesn't crash but doesn't show me anything either!
Now I'm trying with Toast but it still isn't found.
Can someone help me?
This is the main activity:
package com.example.simplenotification;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int MY_NOTIFICATION_ID = 0;
private int mNotificationCount;
private final CharSequence tickerText ="this is tickerText";
private final CharSequence contentTitle="this contentTitle";
private final CharSequence contentText="this coontentText";
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
RemoteViews mContentView = new RemoteViews("com.example.simplenotification.StatusBarWithCustomView", R.layout.custom_view);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationIntent = new Intent(getApplicationContext(),AppGet.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startNotification();
}
});
}
public void startNotification(){
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle("titolo content")
.setContentText("content text")
.setContentIntent(intent);
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
mNotificationManager.notify(MY_NOTIFICATION_ID,notificationBuilder.build());
}
}
This the Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplenotification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/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>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
I don't know how debug the app autorun on emulator. I'm sorry
EDIT: I put in another class the reciever and changed the 'android name'
package com.example.simplenotification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("android.intent.action.MAIN");
context.startService(i);
Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();
}
}
}
Not 100% sure, but try changing this
Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();
into
Toast.makeText(context.getApplicationContext() , "saranno 3?" , Toast.LENGTH_SHORT).show();
Make a separate class for BootUpReceiver. And in manifest pass correct path of ur package name in android:name while defining receiver
<receiver android:name=".receivers.BootUpReceiver"
Update->
Also use this permission in your manisfest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

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.

Creating an action on notification being pressed, Android

So cant seem to get this configured or running properly.
Trying to get the notification to open the MainActivity3 file and then to have it run whatever I want, at the minute just aiming for it to play a sound.
Completely new to android development but think Im along the right lines.
You will see I have two approaches but cant get either to work.
MainActivity.java
package com.example.firstapp;
import android.media.MediaPlayer;
//import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
//import android.app.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//vibrate controls
//Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.follownavi)
.setContentTitle("Hey")
.setContentText("");
PendingIntent pendingIntent;
//Intent intent = new Intent();
Intent intent= new Intent(MainActivity3.onNewIntent(), MainActivity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.setClass(getApplicationContext(),MainActivity3.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
mBuilder.addAction(R.drawable.follownavi,"LISTEN",pendingIntent);
mBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
#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;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//get touch location
int x = (int)event.getX();
int y = (int)event.getY();
//put on screen
TextView tester = (TextView) findViewById(R.id.textView1);
tester.setText("this is x:" + x + " and this is y:" + y);
//move image
ImageView iv = (ImageView) findViewById(R.id.NaviFollow);
if(y > 222)
{
iv.setX(x - 125);
iv.setY(y - 350);
//cause vibration
Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
v.vibrate(300);
}
return true;
}
}
Once the notification is called and appears its meant to run this file
MainActivity3
package com.example.firstapp;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MainActivity3 extends Activity {
protected Context onNewIntent()
{
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(), R.raw.listen);
mp2.start();
return null;
}
}
The error seems to be that the call is static but it wants void, very confused
Error code
Cannot make a static reference to the non-static method onNewIntent() from the type MainActivity3 MainActivity.java /firstApp/src/com/example/firstapp line 38
and if I change the method to fit then the mediaplayer getApplicationContext wont work
after doing more research I feel the manifest might be stopping it but still unsure to the structure of android.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/navibutton"
android:label="Navi"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Please help as getting really boged down and tempting to give up
You are not doing anything with PendingIntent. You should set it in Notification Builder before calling build()
mBuilder.setContentIntent(pendingIntent);
Also you must be aware that creating a new Activity and doing something on onCreate is not a good idea for executing an operation. If you want, for example to show a toast in the same activity you can set specific data in Intent and do it in onNewIntent().
The accepted answer has an example for configuring handling of new intent.
Android: new Intent() starts new instance with android:launchMode="singleTop"

Categories

Resources