If I press the share button that appears by long pressing the text, I want to send the text to other activities of my app. I move to another activity, but the text I want is not delivered. What is the problem?
This is the code of the activity that writes the text I want to deliver.
public class MainActivity extends AppCompatActivity {
EditText editText;
Intent sendIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendIntent.putExtra("TEXT", editText.getText().toString());
sendIntent.setType("text/*");
}
});
}
}
This is the intent-filter of my xml code.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType = "text/*"/>
</intent-filter>
This is the part that receives the intent of the activity that I want to receive data from.
Intent receiveIntent = getIntent();
url = receiveIntent.getStringExtra("TEXT");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
This is the intent-filter part of the xml code of the activity where I want to receive data.
<activity android:name=".MyWebBrowser"
android:label="MyWebBrowser">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/*"/>
</intent-filter>
Here is a simple example for you:
Instead of long press on editText I simply use a button, but nevertheless, the logic stays almost the same:
// MainActivity. From here we send the text from EditText to SecondActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
EditText editText = findViewById(R.id.editText);
Intent intent = new Intent(this, SecondActivity.class);
intent.setAction("MyIntentAction");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent.putExtra("TEXT", editText.getText().toString());
startActivity(intent);
finish();
}
});
}
}
// SecondActivity. Receives and displays text from MainActivity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
Intent intent = getIntent();
if(intent.getAction().equals("MyIntentAction")) {
String str = intent.getStringExtra("TEXT");
textView.setText(str);
}
}
}
// For Long Click detection add the following code to MainActivity:
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
intent.putExtra("TEXT", editText.getText().toString());
startActivity(intent);
finish();
return true;
}
});
I see that you are using two activity has LAUNCHER with the MAIN in your manifest. Please use only one in your manifest.
To pass the data to another activity in your app, you can try to use startActivity and put your data in the intent data:
On action to open the MyWebBrowser:
val intent = Intent(context, MyWebBrowser::class.java)
intent.putExtra("TEXT", yourtext)
startActivity(intent)
Get the Text in your MyWebBrowser:
override fun onCreate(savedInstanceState: Bundle?) {
...
val TEXT = intent.getStringExtra("TEXT")
textView.text = TEXT
}
Related
I created one app with two activities when I run app It opens first launcher activity In this activity I added textViewon clicking this textViewsecond activity is opened,
At the second activity again I added one textView on clicking that textView I expected to launch my first activity(Launcer activity) but this doesn't happens? Why? My Manifest file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adbs.abs.dhanagarmaza" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".LoginRegi"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Register"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="com.adbs.abs.REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent("android.intent.action.MAIN");
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent("com.adbs.abs.REGISTER");
startActivity(openRegister);
}
});
}
Activity 1:-
public class MainActivity extends Activity {
TextView tvOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOne=(TextView)findViewById(R.id.tvOne);
tvOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
});
}
}
Activity 2:-
public class Main2Activity extends Activity {
TextView tvTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvTwo=(TextView)findViewById(R.id.tvTwo);
tvTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
}
}
Intent i =new Intent(this,MainActivity.class);
startActivity(i);
you can post your code so we can get exact problem because ..
by finish(); you can get your prevous activity back either you can do it by Intent
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent(Register.this,LoginRegi.class);
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent(LoginRegi.this,Register.class);
startActivity(openRegister);
}
});
}
This is a simple operation and seems to be working until I've changed my minSdk = 8 to 9 , and targetSdk remains 21 in my manifest.
Problem is that i have an activity A , and i am going to activity B when a button is pressed in activity A . Now on activity B whenever someone press back button i want to clear the activity stack and transfer my activity B to activity C . but instead it finish activity B and go to Activity A , so far i have tried onKeyDown , onBackPressed , nothing seems to work , kindly help.
Activity A (in onClick method):
Intent in = new Intent(A.this,
B.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(in);
[UPDATED]Activity B :
#Override
public void onBackPressed() {
Intent in = new Intent(this, C.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Scores", score);
in.putExtras(bundle);
startActivity(in);
}
Activity C:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.C);
ScoreSaver scores = (ScoreSaver) getIntent().getExtras().getSerializable("Scores");
}
Add android:noHistory="true" to both Activity A and B in your manifest file. This way these activities won't be in your back stack.
In Activity B you have to override the behavior of onBackPressed().
It has to look like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
If you leave super.onBackPressed() in your method, the default behavior will happen, i.e. Activity B will be closed and you jump back to Activity A.
Issue was with Serializable object which was using storing context which will be null in next activity , which causes exception , that crashes my activity. so i removed it everything works fine. Thanks #Chris Fox
Unfortunately I do not have a Git Account, so I post the files' content here. (The app's theme inherits from Theme.AppCompat.Light.DarkActionBar)
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fox.chris.activitytest">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".ActivityA"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityB"
android:noHistory="true">
</activity>
<activity android:name=".ActivityC">
</activity>
</application>
</manifest>
Activity A:
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
}
Activity B:
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
}
Activity C:
public class ActivityC extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
}
}
Im trying to create a notification from an edittext and broadcast receiver. In my first Activity the user should input a message and push the broadcast button. I want to take that string and create a notification from it and open a new activity that displays the message. I am doing all the notification work in my broadcast receiver class.
I have looked around onlne at examples and other peoples code but im not sure what im not getting right. The application loads up just fine and the broadcast button sends the broadcast to the receiver and Logs the string but the notification is never created.
Thanks for any help.
Broadcast class that sends broadcast message:
public class BroadcastReceiverActivity extends Activity
{
EditText et;
Button btn1;
public static String BString = "HappyHemingway";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast_receiver);
et = (EditText)findViewById(R.id.et1);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = et.getText().toString();
send(message);
}
});
}
/*
* This function creates an intent and
* sends a broadcast from the message
* parameter passed in.
*/
protected void send(String msg)
{
Log.i("msg", msg);
Intent i = new Intent();
i.putExtra("message",msg);
i.setAction(BString);
sendBroadcast(i);
}
}
Receiver class that creates notification:
public class Receiver extends BroadcastReceiver
{
// #SuppressLint("NewApi")
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action!=null&&action.equals("HappyHemingway"))
{
String msg = intent.getStringExtra("message");
Log.i("Received",msg);
Intent i = new Intent(context,ViewNotification.class);
i.putExtra("message",msg);
PendingIntent pi = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(context).
setSmallIcon(0).setAutoCancel(true).setTicker(msg).
setWhen(System.currentTimeMillis()).setContentTitle("New Notification!").
setContentText(msg).setContentIntent(pi);
NotificationManager mgr = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = builder.build();
mgr.notify(0, n);
Log.i("Received again",msg);
}
}
}
notification viewer class that is never launched
public class ViewNotification extends Activity
{
String text;
TextView txttext;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.viewnotification);
NotificationManager notificationmanager;
notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.cancel(0);
Intent i = getIntent();
text = i.getStringExtra("message");
txttext = (TextView) findViewById(R.id.text);
txttext.setText(text);
Log.i("made it", "made it made it made it");
}
}
manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".BroadcastReceiverActivity"
android:label="#string/app_name" >
<action android:name="android.intent" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
<activity android:name=".ViewNotification"></activity>
<receiver android:name="Receiver">
<intent-filter>
<action android:name="HappyHemingway">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Hopefully its just a simple error I'm overlooking.This is my first time using Android Studio instead of Eclipse but I dont see how that could make any difference under than my unfamiliarity with the IDE.
Anything helps
thanks.
I'm not sure why I had setSmallIcon(0.)
When I changed it to setSmallIcon(R.drawable.ic_launcher) everything worked fine.
Basically I'm just trying to run a class which is called NotificationReceiverActivity from another Activity class, but nothing shows up on the screen when I click on the button to run the activity class, I feel like I'm being a total noob on the xml part of the code and here is the AndroidManifest.xml ;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.notificationmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="de.vogella.android.notificationmanager.CreateNotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="de.vogella.android.notificationmanager.NotificationReceiverActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The main activity which is CreateNotificationActivity class, works and turns on flawlessly.
Java code for the main activity;
public class CreateNotificationActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(0, noti);
}
}
And the NotificationReceiverActivity class;
public class NotificationReceiverActivity extends Activity implements OnClickListener {
private final String CLASSNAME = getClass().getSimpleName();
Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wl.acquire();
initialize();
ib1.setOnClickListener(this);
Log.i(CLASSNAME, "CREATING NOW"+cam);
}
private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (cam == null) {
cam = Camera.open();
para = cam.getParameters();
para.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(para);
Log.i(CLASSNAME, "AA"+cam);
} else {
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
Log.i(CLASSNAME, "BB"+cam);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
cam=cam;
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
cam=cam;
}
}
And the top 4 lines of the log cat;
01-31 06:11:10.582: E/AndroidRuntime(29533): FATAL EXCEPTION: main
01-31 06:11:10.582: E/AndroidRuntime(29533): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.vogella.android.notificationmanager/de.vogella.android.notificationmanager.NotificationReceiverActivity}: java.lang.SecurityException: Neither user 10181 nor current process has android.permission.WAKE_LOCK.
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
Remove the intent filter from your NotificationReceiverActivity.
to use a button to start NotificationReceiverActivity from you CreateNotificationActivity you have to assign a clicklistener to that button in you activity
This is an example of this
public class CreateNotificationActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(CreateNotificationActivity.this,NotificationReceiverActivity.class);
startActivity(intent);
}
});
}
}
I want the user to be able to tap a button and be taken to a different activity. I've used similar code before in another app, but every time I press the button now the app crashes. In the main menu I have:
Button testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("de.vogella.android.c2dm.simpleclient.TEST"));
}
});
In the manifest:
<activity
android:name=".TestClass"
android:label="#string/app_name" >
<intent-filter>
<action android:name="de.vogella.android.c2dm.simpleclient.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
TestClass.java is:
package de.vogella.android.c2dm.simpleclient;
import android.app.Activity;
import android.os.Bundle;
public class TestClass extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Try this: In the onClick modify your first class to this:
Button testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
final ClassName changeAct = this;
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(changeAct, TEST.class); //Change TEST.class to the name of the class you want it to go to.
startActivity(i);
stop();
}
});
Intent intent = new Intent (CurrentActivity.this, TestClass.class);
startActivity(intent);
If your TestClass is in another package just put your package in front.
Intent intent = new Intent (CurrentActivity.this, de.vogella.android.c2dm.simpleclient.TestClass.class);
startActivity(intent);
Declare the activity in manifest like this:
<activity
android:name="de.vogella.android.c2dm.simpleclient.TestClass"
</activity>