How to use pending intent to Toast something after a specified time when an app closes? Is there any other ways to do it?
I tried Broadcast receiver but don't know how to proceed.
public class Broad extends BroadcastReceiver {
#Override
public void onReceive(Context c, Intent i) {
// TODO Auto-generated method stub
i= new Intent(Intent.CATEGORY_HOME);
PendingIntent pi = PendingIntent.getBroadcast(c, 0, i, 0);
Toast.makeText(c, getResultData(), Toast.LENGTH_SHORT).show();
}
}
What to do next? Please help. I don't understand the tutorials as I'm new to android.
try this method:
public static void toastInTenSeconds(final Context context, final String text) {
new Thread(new Runnable() {
#Override
public void run() {
SystemClock.sleep(1000 * 10);
try {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// Do something
}
}
}
}
Call it from your Actvitiy onDestroy or onStop.
Related
I made an service that triggers every 10 sec. How to connect service to activity when it triggers. Example i refresh my local DB, when appears an update Activity send an Toast.
AlarmService.class
#SuppressLint("SimpleDateFormat")
public class AlarmService extends Service {
Handler mHandler;
private PendingIntent pendingIntent;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
}
public void f() {
Toast t = Toast.makeText(this, "Service is still running",
Toast.LENGTH_SHORT);
t.show();
}
#Override
#Deprecated
public void onStart(Intent intent, int startId) {
Toast t = Toast.makeText(this, "Service started", Toast.LENGTH_SHORT);
t.show();
// TODO Auto-generated method stub
super.onStart(intent, startId);
mHandler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
f();
mHandler.postDelayed(this,10000);
}
};
mHandler.postDelayed(r, 10000);
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(this,AlarmService.class);
startService(serviceIntent);
}
}
Use broadcast receiver like this
public static class MyExtBroadcastReceiver extends BroadcastReceiver {
public MyExtBroadcastReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
//Call your activity here
}
Make a method for setting the alarm
public void setAlarm(){
f(); // call your method f() here
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent alarmintent1 = new Intent(this, MyExtBroadcastReceiver.class);
PendingIntent sender1=PendingIntent.getBroadcast(this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
try {
am.cancel(sender1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ex....."+e);
}
Calendar cal=Calendar.getInstance();
cal.add(Calendar.Seconds,10);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*10*60, sender1);
Call this method from OnCreate()
#Override
public void onCreate() {
setAlarm();
}
}
You can use LocalBroadcastManager, Messenger, ResultReceiver to send data from service to Activity.
Here is an example that uses ResultReceiver to send updated data from Service to Activity.
This questions is similiar to How to use getApplicationContext in BroadcastReceiver class?.
But i didn't know how is the previous activity of his doing. So i didn't know how to resolve mine.
This is my activity :
public class backgroundApplication extends Activity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.background_application);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(backgroundApplication.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(backgroundApplication.this, 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
for the complete code, i've got it from here : http://javatechig.com/android/repeat-alarm-example-in-android
And this is my AlarmReceiver.class extends BroadCastReceiver implements IndoorAtlasListener :
public class AlarmReceiver extends BroadcastReceiver implements IndoorAtlasListener
{
#Override
public void onReceive(Context context, Intent intent) {
initIndoorAtlas();
}
private void initIndoorAtlas() {
try {
mIndoorAtlas = IndoorAtlasFactory.createIndoorAtlas(
getApplicationContext, // this is the redline
this, // IndoorAtlasListener
mApiKey,
mApiSecret);
} catch (IndoorAtlasException ex) {
Log.e("IndoorAtlas", "init failed", ex);
}
}
Anyone can help me ?
in you onReceive method
initIndoorAtlas(context);
and in method
private void initIndoorAtlas(Context context) {
try {
mIndoorAtlas = IndoorAtlasFactory.createIndoorAtlas(
context, // this is the redline
this, // IndoorAtlasListener
mApiKey,
mApiSecret);
} catch (IndoorAtlasException ex) {
Log.e("IndoorAtlas", "init failed", ex);
}
}
If method is form implementing interface then use this code..
private Context context:
and onReceive Method
this.context = context;
and in you method use context as it is global variable we can access it.
I need to use Context object in the function killCall, but I don't know how to pass the Context object to KillCall, could you help me? Thanks!
public class ReceiverCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent msgIntent = new Intent(context, InternetServerCall.class);
context.startService(msgIntent);
}
}
public class InternetServerCall extends IntentService{
public InternetServerCall(String name) {
super("InternetServerCall");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
HandleCall.killCall(context); //Need context
}
}
public class HandleCall {
public static boolean killCall(Context context) {
try {
....
Toast.makeText(context, "PhoneStateReceiver kill incoming call Ok",Toast.LENGTH_SHORT).show();
} catch (Exception ex) { // Many things can go wrong with reflection calls
return false;
}
return true;
}
}
You can get Context into InternetServerCall by doing InternetServerCall.this. And this is because all Android Components overrides Context class, on of them is IntentService.
You can also use getApplicationContext() into IntentService to get context. You can read my another similar answer Pass a Context an IntentService.
But you can not display Toast from IntentService directly, because it needs UI thread but IntentService runs into background thread. You need to use Handler to show Toast, like below example
public class TestService extends IntentService {
private Handler handler;
public TestService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public TestService () {
super("TestService");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
return super.onStartCommand(intent, flags, startId);
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Handling Intent..", Toast.LENGTH_LONG).show();
}
});
}
}
An IntentService is subclass of Context so you can pass in this:
#Override
protected void onHandleIntent(Intent intent) {
HandleCall.killCall(this);
}
Hi fellow coders I really need help and I am really out of ideas, tried a lot of ideas and none worked for me....
I am busy with a emission survey application and I am trying to change the tab within the if statement, if the value being broadcasted is 1, it go to the second tab and then to the third automatically... Attached is snippets, please help, I would really be grateful....!
The first image is of me sending the data back to the first tab
(All is still working at this moment)
loadB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(0);
sendData3();
}
private void sendData3() {
take = "1";
Intent dataIntent = new Intent();
dataIntent.setAction("com.example.e3soft.receiver");
dataIntent.putExtra("taken", take);
sendBroadcast(dataIntent);
Toast.makeText(getBaseContext(), "Working", Toast.LENGTH_LONG).show();
}
});
Without the if statement everything works, please help........!
private void receiveData() {
// TODO Auto-generated method stub
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
take = intent.getStringExtra("taken");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.e3soft.receiver");
registerReceiver(receiver, filter);
changeTab();
}
protected void changeTab() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), take, Toast.LENGTH_LONG).show();
try {
if (take.equals("1")) {
populateVariables();
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(1);
sendData();
}
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG)
.show();
}
}
move your this code inside onReceive :
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.e3soft.receiver");
registerReceiver(receiver, filter);
changeTab();
I want to start a new activity in non-Activity class that implements a DialogListener following is my code:
public class FacebookLoginDialog implements DialogListener {
#Override
public void onComplete(Bundle values) {
HomeActivity.showInLog(values.toString());
Intent i1 = new Intent (this, SearchActivity.class);
startActivity(i1);
}
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
}
I can't start the new activity using intent in onComplete method, please help.
Thanks
This doesn't work because you need a Context in order to start a new activity. You can reorganize your class into something like this:
public class FacebookLoginDialog implements DialogListener {
private final Context context;
public FacebookLoginDialog(Context context) {
this.context = context;
}
#Override
public void onComplete(Bundle values) {
HomeActivity.showInLog(values.toString());
Intent i1 = new Intent (context, SearchActivity.class);
context.startActivity(i1);
}
//Other methods...
}
Then it will work.
Pass context as constructor parameter and then try this
Intent i = new Intent(this, SearchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
use starActivity from non-activity class:
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "YOUR STRING");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share via...");
context.startActivity(Intent.createChooser(intent, "Share"));
For Easy Usage you can a method for this particular method:
public class Something
{
public static void navigate(Context context, Class<?> nameOfClass)
{
Intent i = new Intent(context, nameOfClass);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
can be called in other class and method everytime by calling this:
Something.navigate(activityName.this, classYourWantTONavigateTo.class);