Start a new Activity from non Activity class - android

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);

Related

How to create public class method for intent in android studio

I am newbie in app development.In my project have lot of Explicit Intent so i want to make a public method which argument contains conext or a class where i want to jump but it's not working is there have who know the proper solution for it.
Here is my method where i call my public intent class.
private void startActivityMethod() {
if (progressStatus == 100) {
if (firebaseUser != null) {
if (sp.getString(Constants.userType, "").equals("student")) {
Constants.explicitIntent(SplashActivity.this, studentMainActivity.class);
} else if (sp.getString(Constants.userType, "").equals("faculties")) {
Constants.explicitIntent(SplashActivity.this, facultiesMainActivity.class);
}
} else {
startActivity(new Intent(SplashActivity.this, LoginActivity.class));
finish();
}
}
}
Here is my public class method for intent.
public static void explicitIntent(Context context, Class<?> intentClass) {
Intent intent =new Intent(context,intentClass);
}
You should call startActivity to launch the intent
public static void explicitIntent(Context context, Class<?> intentClass) {
Intent intent =new Intent(context, intentClass);
context.startActivity(intent);
}

Intent Not working not landing to one.class page Below is the code

public class one extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(),"lastpage",Toast.LENGTH_SHORT).show();
sendSms();
}
public void sendSms() {
Intent intent=getIntent();
String SMS=intent.getStringExtra("Message");
Intent intent1=getIntent();
String Contact=intent1.getStringExtra("Number");
SmsManager manager=SmsManager.getDefault();
manager.sendTextMessage(Contact, null, SMS, null, null);
Toast.makeText(getApplicationContext(), "SENT", Toast.LENGTH_SHORT).show();
}
}
public class AlarmReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(context,one.class);
Toast.makeText(context, "Alarm is scheduled", Toast.LENGTH_LONG).show();
}
}
In this the Intent is not landing to one.java activity. And If possible please help if can write the sendSms method in Alarm Receiver.java activity
In your AlarmReceiver you never actually start the one activity. You also never add the extras you intend to pull out of it.
Try this:
Intent intent1 = new Intent(context,one.class);
intent1.putExtra("Message", "foo");
intent1.putExtra("Number", 123456789);
startActivity(intent1 );
You need to add startActivity(intent1); in the onReceive method
In your AlarmReciever, you missed out to start the called intent. It's an simple issue. Below i have given the corrected code
public class AlarmReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(context,one.class);
startActivity(intent1);
Toast.makeText(context, "Alarm is scheduled", Toast.LENGTH_LONG).show();
}
}

How to getApplcationContext using BroadCastReceiver

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.

Passing data from intent service to activity

I have a broadcast receiver from which am calling intentservice.I want to send the data received in the intentservice to a activity.String s=extras.getString("Notice").I want to send this received string to a new activity
#Override
protected void onHandleIntent(Intent arg0) {
Intent dialogIntent = new Intent(getBaseContext(), myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("value", extras.getString("Notice"));
getApplication().startActivity(dialogIntent);
}
public class YourIntentService extends IntentService {
#Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, YourNewActivity.class);
intent.putExtra("YourStringKey", "yourString");
startActivity(intent);
}
public YourIntentService() {
super("YourIntentService");
}
}
Try this. Sorry for misunderstanding.
public class MyIntentService extends IntentService {
public MyIntentService() {
super(" MyIntentService");
}
#Override
protected void onHandleIntent(Intent arg0) {
String s=arg0.getExtras.getString("Notice")
Intent i = new Intent(this, yourActivity.class);
i.putExtra("Notice",s);
getApplication().startActivity(i);
}
}

Using Pending Intent

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.

Categories

Resources