How can I get the method in my BroadcastReceiver into my Fragment?
Or is it even possible?
receiver:
public class Receiver extends BroadcastReceiver {
public void test(Context c) {
....
}
}
Fragment:
public class Test extends Fragment {
...
}
So a error appeared:
Attempt to invoke virtual method 'android.content.Context
android.content.Context.getApplicationContext()' on a null object
reference
at
com.test.tab.MyReceiver.sendNotificationIfTimeEnd01(MyReceiver.java:47)
at com.test.tab.Juli.broadcastIntent(Juli.java:54)
at com.test.tab.Juli.onCreateView(Juli.java:188)
The code for those lines are:
in Receiver:
public void sendNotificationIfTimeEnd01(Context c) {
Context context = c.getApplicationContext();
Intent intent01 = new Intent(context, MainActivity.class);
....
}
and in Fragment:
receiver.sendNotificationIfTimeEnd01(context);
broadcastIntent();
EDIT UPGRADED CODE:
Fragment:
public class FragmentTest extends Fragment {
private Context context;
IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();
// Call this method when the condition is met.
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
getActivity().sendBroadcast(intent);
}
...
if (diffDays <= 0 && diffHours <= 0 && diffMinutes <= 0) {
((TextView) android.findViewById(R.id.test)).setText("test works");
if (!notification043 && !buttonColor01.equals("red01")) {
broadcastIntent();
editor.putBoolean("notification43", true);
editor.apply();
}
}
}
MyReceiver:
public class MyReceiver extends BroadcastReceiver {
public static final int NOTIFICATION_ID_01 = 1;
#Override
public void onReceive(Context context, Intent intent) {
sendNotificationIfTimeEnd01(context);
}
public void sendNotificationIfTimeEnd01(Context c) {
Context context = c.getApplicationContext();
Intent intent01 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent01 = PendingIntent.getActivity(context, 1, intent01, 0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentIntent(pendingIntent01)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher))
.setContentTitle(testArray[0])
.setContentText("testready")
.setSubText("click here");
NotificationManager notificationManager =
(NotificationManager) c.getSystemService(c.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_01, builder.build());
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(c.getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Actually, you could use Intents :
public class Test extends Fragment {
...
// Call this method when the condition is met.
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
getActivity().sendBroadcast(intent);
}
}
And declare that your broadcast receiver can react to this kind of Intent either through the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.BroadcastDetector"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.example.Broadcast" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
or programmatically
IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();
getActivity().registerReceiver(receiver, filter);
Then you can intercept this Intent in the receiver :
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
}
}
Related
I would like to notify the incoming sms message. I implemented the BroadcastReceiver for SMS receiver. And I also implemented the service for BOOT_COMPLETED action. Because the receiver cannot be registered if the device is reboot. When I launch the apps, that only call onReceiver once. But I reboot the device and start the BOOT_COMPLETED service. That will make the onReceive method call twice. But I don't know why. I confirm I only register the receiver once.
ps: When I reboot the device, I received the log order is enable receiver, register receiver, enable receive, onReceived, onReceived
Here is my code:
AutoStartService.java
public class AutoStartService extends Service {
private SmsReceiver smsReceiver;
public static boolean isStarted = false;
#Override
public void onCreate() {
super.onCreate();
isStarted = true;
if(!SmsReceiver.isEnableReceiver && smsReceiver == null) {
smsReceiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, intentFilter);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Log.e("AAA", "register receiver");
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
isStarted = false;
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(!AutoStartService.isStarted) {
new Handler().postDelayed(() -> {
Intent service = new Intent(context, AutoStartService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}
}, 500);
}
}
}
SmsReceiver.java
public class SmsReceiver extends BroadcastReceiver implements SmsHelper.SmsHelperDelegate {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
public static boolean isEnableReceiver = false;
public SmsReceiver() {
super();
isEnableReceiver = true;
Log.e("AAA", "enable receiver");
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Log.e("SmsReceiver", "onReceived");
}
}
}
AndroidManifest.xml
<receiver android:name=".receiver.SmsReceiver" android:enabled="true" android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name=".receiver.AutoStartService" android:exported="true"/>
<receiver android:name=".receiver.BootCompleteReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I'm trying to set up a alarm to start a job on a certain time but my alarm is never trigger and I don't understand what i'm missing. I'm using android api 22.
Thank you.
public class MainActivity extends ProtectedActivity {
#Override
public void onResume() {
super.onResume();
if (terminalSettings.GetSettlementEnabled())
{
startAlarmManager();
}
}
private void startAlarmManager()
{
Intent i = new Intent(MainActivity.this,
AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
i, 0);
AlarmManager am =( AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pi); //
Millisec * Second * Minute
}
}
class AlarmBroadcastReceiver
public class AlarmBroadcastReceiver extends BroadcastReceiver {
public AlarmBroadcastReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm !!!!!!!!!!",
Toast.LENGTH_LONG).show(); // For example
}
}
In the manifest I added those line
<receiver android:name=".AlarmBroadcastReceiver"></receiver>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
You need to call registerReceiver
public static final String Broadcast_PLAY_NEW_AUDIO = "Some key so you know which broadcast is which";
//Create Receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Do stuff
}
};
//Register receiver
IntentFilter filter = new IntentFilter(HomeActivity.Broadcast_PLAY_NEW_AUDIO);
registerReceiver(receiver, filter);
//Send Broadcast
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
I want to get data in background and automatic and in the interval of time,
I use this but not work does not work correctly
AndroidManifest.xml
.
.
.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
.
.
.
<service
android:name=".NewsServiceReceiver"
android:icon="#drawable/ic_launcher"
android:label="#string/service_name" >
</service>
<receiver
android:name=".NewsReceiver"
android:enabled="true"
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>
<receiver android:name=".NewsStartReceiver" >
</receiver>
NewsReceiver.java
public class NewsReceiver extends BroadcastReceiver {
private static final long REPEAT_TIME = 1000 * 30;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NewsStartReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
}
}
NewsStartReceiver.java
public class NewsStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent service = new Intent(context, NewsServiceReceiver.class);
context.startService(service);
}
}
NewsServiceReceiver.java
public class NewsServiceReceiver extends Service {
.
.
.
#Override
public IBinder onBind(Intent arg0) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(DBAdapter.TAG, "[*******-----------> BACKGROUND <-----------*******]");
//My Code Here
return Service.START_NOT_STICKY;
}
}
i want run NewsServiceReceiver.java in background when internet connect or app run As well as run automatic in the interval of time!?
my problem solve with
Autostart.java
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent alarm = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
System.out.println("[*******-----------> Autostart <-----------*******]");
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 15000, pendingIntent);
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, NewsServiceReceiver.class);
context.startService(background);
}
}
NewsServiceReceiver.java
public class NewsServiceReceiver extends Service {
private boolean isRunning;
private Context contex;
private Thread backgroundTread;
#Override
public IBinder onBind(Intent intent){
return null;
}
#Override
public void onCreate(){
this.contex = this;
this.isRunning = false;
this.backgroundTread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("[*******-----------> BACKGROUND <-----------*******]");
//------------My Code Here--------------
stopSelf();
}
};
#Override
public void onDestroy(){
this.isRunning = false;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!this.isRunning){
this.isRunning = true;
this.backgroundTread.start();
}
return START_STICKY;
}
}
SplashActivity.java -> May first Activity when app run
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
-
this.context=this;
Intent alarm = new Intent(this.context, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if(alarmRunning == false){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 15000, pendingIntent);
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<service
android:name=".NewsServiceReceiver"
android:enabled="true" >
</service>
<receiver
android:name=".Autostart"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" >
</receiver>
I design alarm manager with repeated in specific time. But when I cancel it on on click method after seconds(time Interval) service start automatically itself.
I want activate service on button click and repeat in interval. and after restaring phone wants to start service too.
any Idea?
here its my manifest code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:enabled="true"/>
<receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
</application>
</manifest>
My AlarmReceiver :
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "BANANEALARM";
private static final int NOTIFICATION_ID = 1;
Intent intent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "starttt", Toast.LENGTH_LONG).show();
// if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d(TAG, "in onreceive");
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
// }
}
}
My BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Toast.makeText(context, "Boot", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
this.scheduleAlarms(context, calendar);
}
}
private void scheduleAlarms(Context ctxt, Calendar c) {
AlarmManager alarManager = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, AlarmService.class);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),getInterval(), pi);
}
private int getInterval(){
int days = 0;
int hours = 0;
int minutes = 1;
int seconds = 60;
int milliseconds = 1000;
int repeatMS = minutes* seconds * milliseconds;
return repeatMS;
}
}
And my MainActivity :
public class MainActivity extends Activity {
private static final String TAG = "BANANEALARM";
public AlarmManager alarmManager;
Intent alarmIntent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
Button btn,btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setAlarm();
}
});
btn2=(Button)findViewById(R.id.stop_s);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopservice();
}
});
}
public void stopservice()
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(MainActivity.this , "Cancel!", Toast.LENGTH_LONG).show();
}
public void triggerAlarm(View v){
setAlarm();
}
public void setAlarm(){
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction("com.example.alarm.ACTION");
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarm = (AlarmManager) MainActivity.this
.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
getInterval(), pendingIntent);
Log.d(TAG,"alarmStartTime: " );
}
private int getInterval(){
int days = 0;
int hours = 0;
int minutes = 1;
int seconds = 60;
int milliseconds = 1000;
int repeatMS = minutes* seconds * milliseconds;
return repeatMS;
}
#Override
protected void onStart(){
super.onStart();
cancelNotifications();
setAlarm();
}
public void cancelNotifications(){
notificationManager.cancelAll();
}
}
In your stopservicemethod, codealarmManager.cancel(pendingIntent), the pendingIntent is always null.
Try replace
PendingIntent pendingIntent = PendingIntent
to
pendingIntent = PendingIntent
in your setAlarm method.
Hope it helps.
I am working on Alarm Manager. I used a service to start and will ring a tone. But the service is not starting. Can anyone suggest about service, Pending Intent and identify the error in my code. The code is copied from net.
This is my AndroidScheduledActivity.java
public class AndroidScheduledActivity extends Activity {
private PendingIntent pendingIntent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.startalarm);
Button buttonCancel = (Button) findViewById(R.id.cancelalarm);
buttonStart.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(AndroidScheduledActivity.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidScheduledActivity.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
Toast.makeText(AndroidScheduledActivity.this, "Start Alarm",
Toast.LENGTH_LONG).show();
Log.d("Alarm", "Alarm Started");
}
});
buttonCancel.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(AndroidScheduledActivity.this, "Cancel!",
Toast.LENGTH_LONG).show();
}
});
}
}
And this is my MyAlarmService.java
public class MyAlarmService extends Service {
#Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("AlarmService", "Alarm Starting to ring");
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getBaseContext(), alert);
mp.setVolume(100, 100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(400);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
And my menifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm_basic"
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" >
<activity
android:name="com.example.alarm_basic.AndroidScheduledActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyAlarmService" />
</application>
</manifest>
Also need to check wheather Ringtone is null also.
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(getBaseContext(), alert);
if(r == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
r = RingtoneManager.getRingtone(getBaseContext(), alert);
if(r == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getBaseContext(), alert);
}
}
if(r != null)
r.play();
Or otherway
MediaPlayer mp = MediaPlayer.create(getBaseContext(), alert);
if(mp !=null) {
mp.setVolume(100, 100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
Try it, and let me know what happen...
In your manifest.xml file
<service android:enabled="true" android:name=".ServiceClass" />
Add a permission in your Manifest file
< uses-permission android:name="android.permission.VIBRATE" />
Should check vibrator is null or not.
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (vibrator != null)
vibrator.vibrate(400);