i built a code that switch from one activity to another.
when run this code it prints following errors.
Error in an XML file: aborting build.
and
Installation error: INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID
here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wishme.code"
android:versionCode="1"
android:versionName="1.0" android:sharedUserId="#string/app_name">
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".WishMeActivity"
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=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
here is activity 1
package wishme.code;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.Intents;
import android.widget.TextView;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class WishMeActivity extends Activity {
private EditText text;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(EditText) findViewById(R.id.editText1);
}
public void myClickhandler(View view)
{
switch(view.getId())
{
case R.id.button1:
RadioButton celsiusbutton=(RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheit=(RadioButton) findViewById(R.id.radio2);
if(text.getText().length()==0)
{
Toast.makeText(this,"Enter a Valid Value",Toast.LENGTH_LONG).show();
return;
}
float input=Float.parseFloat(text.getText().toString());
if(celsiusbutton.isChecked())
{text.setText(String.valueOf(convertFahrenheitToCelsius(input)));
celsiusbutton.setChecked(false);
fahrenheit.setChecked(true);}
else
{
text.setText(String.valueOf(convertCelsiusToFahrenheit(input)));
fahrenheit.setChecked(false);
celsiusbutton.setChecked(true);
}
break;
case R.id.button_nxt:
Intent myintent=new Intent(view.getContext(),Activity2.class);
startActivityForResult(myintent, 0);
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
and finally this is activity 2
package wishme.code;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity{
public void onCreate(Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
setContentView(R.layout.main2);
Button previous=(Button) findViewById(R.id.button_prev);
}
public void prevclickhandler(View view)
{
Intent intent=new Intent();
setResult(RESULT_OK,intent);
finish();
}
}
please suggest me the solution.
I you just want to go from one activity to another, then you dont need to give the sharedUserID. Its used in cases where you have two different applications that are signed with same certificate and you want to share data between them or you want them to run in the same process.
If you remove it, it should solve your problem.
For information about sharedUserID: http://developer.android.com/guide/topics/manifest/manifest-element.html#uid
This may be due to your sharedUserId value being of a non-permitted value. It has to have the same format as the package structure (e.g. com.android).
Related
I'm trying to create a Service that checks every x minutes for new messages on a server.
I was quit happy to found this post : Alarm Manager Example
MainActivity
package com.example.alarmexample;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startAlert();
} public void startAlert() {
int timeInSec = 2;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (timeInSec * 1000), pendingIntent);
Toast.makeText(this, "Alarm set to after " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
mp=MediaPlayer.create(context, R.raw.alarm);
mp.start();
Toast.makeText(context, "Alarm", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmexample" >
<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="com.example.alarmexample.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:name="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
This does exact the thing I want. But if I try to use the Context from the BroadcastReceiver-class to notify the user. The App doesn't starts when the user clicks on the notification.
Is there something spezial I have to do to archive this ?
This is not a direct answer to your question (cant comment because not enough reputation) , but do you really need to check the server every x minutes for an event? I dont know anything about your project, but firebase https://firebase.google.com/docs/cloud-messaging/ would probably be a good alternative. It allows you to send Push Notifications to your client so that the client doesnt need to poll constantly.
Open application after clicking on Notification
Here is the answer - I was looking at the wrong end from this problem.
I am developing an android app which should be able to identify when someone press the hardware volume up button of the phone , and give a notification saying "volume up button has pressed".
This is my BroadcastReceiver class.
package com.example.volumebut;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;
public class HardwareButtonReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
KeyEvent e = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if(e.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
Toast.makeText(context, "volume up button pressed." ,Toast.LENGTH_LONG).show();
}
}
}
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volumebut"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.volumebut.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:name=".HardwareButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
Since I am new to android development I have no idea how to implement MainActivity.java file.
This is the MainActivity.java file I have wrote so far. But it gives an error saying "The method registerMediaButtonEventReceiver(HardwareButtonReceiver) is undefined for the type MainActivity"
package com.example.volumebut;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
HardwareButtonReceiver receiver = new HardwareButtonReceiver();
registerMediaButtonEventReceiver(receiver);
}
}
If you guys can help me to solve my problem I appreciate It much.
http://developer.android.com/reference/android/view/KeyEvent.html
go through above link you will get different key events
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//
}
else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
}
return super.onKeyDown(keyCode, event);
}
You must add method registerMediaButtonEventReceiver to your activity class.
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml_of_your_activity);
HardwareButtonReceiver receiver = new HardwareButtonReceiver();
registerMediaButtonEventReceiver(receiver);
}
private void registerMediaButtonEventReceiver(HardwareButtonReceiver receiver) {
}
}
And then implement it of course.
This is my main page code:
package com.example.splasher;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public static String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// hide statusbar of Android
// could also be done later
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText field= (EditText) findViewById (R.id.editText1);
field.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) ((EditText)v).selectAll();
}
});
text=field.getText().toString();
try{
startActivity(new Intent("com.example.splasher.View"));
}
catch(ActivityNotFoundException e){
}
}
});
}
}
This is the code of my called activity:
package com.example.splasher;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class View extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// hide statusbar of Android
// could also be done later
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.view);
// TextView textview=(TextView) findViewById (R.id.textView1);
// textview.setText(MainActivity.text);
}}
And this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splasher"
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=".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=".View"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.splasher.View" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I can't work out why I get this error. Any ideas? Names are all the same and with same upper, lower case letters. They are all described in manifest as well.
In your manifest, type the activity action in caps.
< action android:name="COM.EXAMPLE.SPLASHER.VIEW" / >
this is done for all activities except for the main activity
There is MainActivity.this.startActivity(new Intent(MainActivity.this, Views.class)); Views class with 's' !
there is no need of the intent filter which you have applied in the View activity..Just keep one intent filter in your MainActivity
Replace the start activity line code with following:
startActivity(new Intent(MainActivity.this,View.class));
Try to replace your startActivity call by MainActivity.this.startActivity in your MainActivity.
I think there is a scope trouble.
I am trying to open a web browser window onclick of a button.
My click event doesn't appear to be getting called. LogCat shows no errors or any evidence of it being called. I have a feeling this is due to me having 2 methods named 'onClick', but removing the second method causes an error. I can fix this error by making MainActivity abstract, but that crashes the app.
I feel this is a simple fix, but after pouring over documentation and several tutorials I cannot find the answer.
Code is below, followed by my manifest. Thanks in advance.
package com.spotsofmagic.spotsofmagic;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "Activity...";
private NfcAdapter mAdapter;
private TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// grab our NFC Adapter
mAdapter = NfcAdapter.getDefaultAdapter(this);
// TextView that we'll use to output messages to screen
mTextView = (TextView)findViewById(R.id.text_view);
//displayMessage("Loading payload...");
}
private void displayMessage(String message) {
mTextView.setText(message);
}
public void onClick(View v) {
// TODO Auto-generated method stub
// do something when the button is clicked
displayMessage("Loading broswer");
Uri uriUrl = Uri.parse("http://www.***.com/");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
if(v.getId() == R.id.btnVisitWebsite) {
}
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spotsofmagic.spotsofmagic"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<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>
<activity
android:name=".CardActivity"
android:label="#string/app_name" >
<!-- Handle a collectable card NDEF record -->
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="application/vnd.spotsofmagic.spotsofmagic"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
You have an OnClickListener but you didn't attach it to any button. Try
((Button) findViewById(R.id.your_button_id)).setOnClickListener(this);
EDIT: The other problem is, that you implemented DialogInterface.OnClickListener instead of View.OnClickListener in your Activity.
Remove the imports
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
And add
import android.view.View.OnClickListener;
You can also remove
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
after this.
Because you didn't associate the click event.
If you are binding it to button (or) something, you need to do bind it for 'this'
Example:
button.setOnClickListener(this);
To avoid confusion, you could also do it using the XML if you are not creating the view programatically. You can do something like this.
<Button android:width="20dp" android:height="20dp" android:onClick="openBrowser" />
and provide the method in your program as
public void openBrowser(View v)
{
/* do your stuff here */
}
I've found several tutorials about setting the alarm receiver to send a toast message in set intervals. and i've been following the code and broken down my own project into 3 classes.
the HelloDroidActivity.java is:
package com.example.helloandroid;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.helloandroid.alarms.MyAlarmReciever;
public class HelloDroidActivity extends Activity {
/** Called when the activity is first created. */
public static int RTC_WAKEUP;
public static long INTERVAL_FIFTEEN_MINUTES;
private AlarmManager alarmMgr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Krishneel");
setContentView(tv);
Toast.makeText(this, "Alarm went off", Toast.LENGTH_SHORT).show();
Log.d("OnCreate", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 7000, pendingIntent);
}
}
also the MyAlarmReciever.java(i am already aware of the spelling mistake on the name):
package com.example.helloandroid.alarms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyAlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("onReceive", "ladskjflsakjdflskjdflskjdfslkjdflasdf");
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
}
}
and the Android Manifest which looks like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.helloandroid.HelloDroidActivity"
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="AlarmReceiver">
<intent-filter>
<action android:name="com.example.helloandroid.alarms" />
</intent-filter>
</receiver>
</application>
</manifest>
I have read that in order to get the project to receive my alarmReceiver java class I need to edit the manifest with a new receiver. but I'm fairly new to XML and dont know which direction to take.
There is already a receiver that you have defined in your manifest file. But the name is not correct see the name needs to be the full class name i.e the package.RecieverName. And in your case the name of your receiver is MyAlarmReciever. So the receiver will be defined as follows
<receiver android:name=".alarms.MyAlarmReciever">
<intent-filter>
<action android:name="com.example.helloandroid.alarms" />
</intent-filter>
</receiver>
In your manifest, the receiver is listening to an action called com.example.helloandroid.alarms. But in your HelloDroidActivity.java there is not such action is added to the intent.
public class HelloDroidActivity extends Activity {
//....
#Override
public void onCreate(Bundle savedInstanceState) {
//....
Intent intent = new Intent(this, MyAlarmReciever.class);
intent.setAction("com.example.helloandroid.alarms");
//....
}
}