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"
Related
I want to create an alarm to create a notification.
The app that will create to Intent is not the same that will receive it.
I have that code, but it doesn't work, and I can't figure why :
First app:
package io.github.alucas.alarmsend;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Create alarm", Toast.LENGTH_SHORT).show();
final Intent intent = new Intent("io.github.alucas.alarmreceive.ALARM");
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 5, alarmIntent);
Toast.makeText(this, "Send alarm", Toast.LENGTH_SHORT).show();
}
}
Second app :
package io.github.alucas.alarmreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receive alarm", Toast.LENGTH_SHORT).show();
}
}
Manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.alucas.alarmreceive">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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="io.github.alucas.alarmreceive.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="io.github.alucas.alarmreceive.ALARM"/>
</intent-filter>
</receiver>
</application>
</manifest>
[edit1]
Adding the flowing line fixed my problem :
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Your second application has no activity. If you add an activity to your second app, install and run on the device, your code will work and the alarm will be delivered to the second application.
Adding the folowing line solved my problem :
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
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"/>
I need to run my app as the user clicks on the unlock screen. I have used ACTION_USER_PRESENT Intent in a BroadcastReceiver to check. I used the following code. I need to show my app before the unlock screen. But my app is visible after I unlock the screen. Any help will be appreciated.
My BroadcastReceiver
package com.progressindicator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, ProgressIndicator.class);
s.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
// s.setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
s.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.progressindicator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.progressindicator.ProgressIndicator"
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=".Receive" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
</manifest>
My Activity:
package com.progressindicator;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProgressIndicator extends Activity {
ImageView loading;
AnimationDrawable loadAnimation;
private Window wind;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_progress_indicator);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// requestWindowFeature(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// wind = this.getWindow();
// wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
// wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
setContentView(main);
loading = new ImageView(this);
loading.setImageResource(R.drawable.loading);
loadAnimation = (AnimationDrawable)loading.getDrawable();
main.addView(loading);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
loadAnimation.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.progress_indicator, menu);
return true;
}
}
Try adding WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED for your activity to be started, this will temporarily disable keyguard/keylock as long as your activity is on top of all other windows.
I used this for a project some time ago
// set flags for staying on and lockscreen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
Also I would check your Intent parameters to make sure your creating a new Activity and onCreate is called.
If you want to disable the lock screen, you will need to be a Device Administrator.
EDIT:
ACTION_USER_PRESENT is sent after the user unlocks the screen, that's why it doesn't works for showing a screen on top of the lockscreen.
You should change to ACTION_SCREEN_ON.
Edit 2:
On the manifest I have android:launchMode="singleTask" and in the intent I call the activity with:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
You need to add WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED to your
window flags, as in Activity.getWindow().addFlags(...).
I recommend to use this in addition with FLAG_WATCH_OUTSIDE_TOUCH and call
it in your onCreate lifecycle method.
To the "security hole" topic. It is correct that as an app developer you are
able to pop up before the security lockscreen appears, but a simple
home button tap will pause your activity (even a System-Alert) and show the
secure guard.
I know that there are some hacky ways around this, but they do not work
with every device, are somewhat broken on HTC and Samsumg and no app should
be able to lock out the user.
Steve
my app force closes I do not know why. All it should do is vibrating when it is in the foreground and stop if it is not but when I start it on my phone it force closes. I added lines about vibrator usage into the manifest by hand. Maybe this is the problem. I have to anything else with it to be valid for the app?
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kevinboonemidi"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="android.permission.VIBRATE" ></permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.haptic.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>
My code:
package com.example.haptic_sof;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.example.kevinboonemidi.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity
{
protected Vibrator v = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long pwmCycles[];
pwmCycles = new long[4];
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 200, 10 };
v.vibrate(pattern, 0);
}
#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;
}
protected void onPause(){
super.onPause();
v.cancel();
}
protected void onStop()
{
super.onStop();
v.cancel();
}
}
Your packages are a mess.
You define com.example.kevinboonemidi as your package in the manifest, declare your Activity to be in com.example.haptic and actually put your Activity in com.example.haptic_sof.
Change android:name="com.example.haptic.MainActivity" to android:name="com.example.haptic_sof.MainActivity" and your app should run, though you may need to end up using one package entirely at least to start with.
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.