Hi im trying to make it that when a user clicks on on a button the phone vibrates. Heres manifest.xml and AndroidVibrator.java so whats wrong? how can i fix it? thanks!
heres the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".AndroidActivity"
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>
<activity android:name=".activity2"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".activity3"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".next" >
</activity>
</application>
</manifest>
Heres AndroidActivity:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.os.Vibrator;
public class AndroidVibrator extends Activity implements OnClickListener
{
private View myView;
private Vibrator myVib;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
//myView can be any type of view, button, etc.
myView = (View) this.findViewById(R.id.sound);
myView.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View v)
{
myVib.vibrate(50);
//add whatever you want after this
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
Make sure you call
<uses-permission android:name="android.permission.VIBRATE" />
In your manifest
EDIT:
Try putting your permission at the end of the manifest, or after version code.
I've responded to your previous post about the same matter
this time you place the permissions over uses sdk...
check my other answer.
Also check the ouput of LogCat. Does your application force closes?
may be 50 ms is too short to feel vibrate
try more than 50ms
Related
I have tried this on a few android phones with no luck (i did start the app once).
This was mostly generated from android studio 1.3.2 and from some questions like this and that and the other.
Perhaps someone can point out what i am doing wrong.
Thanks
edit: moved receiver inside app, but no joy.
edit2: added missing permissions for receiver. now seems to work on a nexus 4. but not on the kindle fire or the at&t tablets. although it seems to come right away if i press the circle icon on the azpen.
edit3: seems to work sometimes on the fires now.
package acme.startup;
import android.content.*;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
package acme.startup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
</application>
</manifest>
Every Component must be declared inside application tag into manifest. Here you declared receiver outside of application tag. Read Structure of the Manifest File.
Correct manifest wil be
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have to add permission to manifest file. Add this before your <application> tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I am learning the basics of android programming. Now I have made a Life Cycle test. The list activity works fine, but when I attempt to open the Life Cycle test, the app stops and closes. I have got the same problem on several devices. These are my codes:
(Manifest)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.badlogic.androidgames"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".AndroidBasicsStarter"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
(AndroidBasicsStarter.java)
package com.badlogic.androidgames;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AndroidBasicsStarter extends ListActivity {
String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest", "KeyTest",
"AccelerometerTest", "AssetsTest", "ExternalStorageTest", "SoundPoolTest",
"MediaPlayerTest", "FullScreenTest", "RenderViewTest", "ShapeTest", "BitmapTest",
"FontTest", "SurfaceViewTest"
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tests));
}
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
String testName = tests[position];
try {
Class clazz = Class
.forName("com.badlogic.androidgames." + testName);
Intent intent = new Intent(this, clazz);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
(LifeCycleTest.java)
package com.badlogic.androidgames;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LifeCycleTest extends Activity {
StringBuilder builder = new StringBuilder();
TextView textView;
private void log(String text) {
Log.d("LifeCycleTest", text);
builder.append(text);
builder.append('\n');
textView.setText(builder.toString());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText(builder.toString());
setContentView(textView);
log("created");
}
#Override
protected void onResume() {
super.onResume();
log("resumed");
}
#Override
protected void onPause() {
super.onPause();
log("paused");
if (isFinishing()) {
log("finishing");
}
}
}
I use Eclipse on Windows 7. Thank you in advance!
Add your LifeCycleTest activity in AndroidManifest.xml
<activity
android:name=".LifeCycleTest"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Include both your activities inside your Manifest file. It should look something like this.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".AndroidBasicsStarter"
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=".LifeCycleTest"
android:label="#string/app_name" >
</activity>
</application>
I want to do a simple exercise with permissions. I need to define and enforce a custom permission for my app called "DangerousApp" And after that I need to set up another app, "PermissionsLab" in such a way that it could use the "DangerousApp" but when I try to start the "DangerousApp" using "PermissionLab" I get an error: "Unfortunately, PermissionsLab has stopped"
AndroidManifest.xml of PermissionsLab:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="#string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="#string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="#string/title_activity_customization" >
</activity>
</application>
</manifest>
AndroidManifest.xml of DangerousApp:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="#string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="#string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="#string/title_activity_customization" >
</activity>
</application>
</manifest>
And the activity used to start DangerousApp:
package course.labs.permissionslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GoToDangerousActivity extends Activity {
private static final String TAG = "Lab-Permissions";
private static final String DANGEROUS_ACTIVITY_ACTION = "course.labs.permissions.DANGEROUS_ACTIVITY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.go_to_dangerous_activity);
Button startDangerousActivityButton = (Button) findViewById(R.id.start_dangerous_activity_button);
startDangerousActivityButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startDangerousActivity();
}
});
}
private void startDangerousActivity() {
Log.i(TAG, "Entered startDangerousActivity()");
startActivity(new Intent(DANGEROUS_ACTIVITY_ACTION));
}
}
Thanks for your clarifications.
Permission help you to protect service, application, activity, ...
Here documentation:
http://developer.android.com/guide/topics/security/permissions.html#declaring
http://developer.android.com/guide/topics/security/permissions.html#manifest
for instance, if you want to protect an activity with a permission, in AndroidManifest declare a permission and put it on activity as this:
<manifest [...] >
<permission android:name="com.mycompany.MY_PERMISSION" android:protectionLevel="normal"
android:description="#string/permission_desc"
android:label="#string/permission_label" />
<application [...] >
<activity [...] android:permission="com.mycompany.MY_PERMISSION">
</activity>
</application>
</manifest>
In another application, if you want to call activity protected by permission, you have to add in AndroidManifest:
<uses-permission android:name="com.mycompany.MY_PERMISSION" />
For my Android App, I've declared multiple Activities in the AndroidManifest.xml file.
The AndroidManifest.xml file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.btdt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/quizicon"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:debuggable="true">
<activity android:name="QuizActivity"
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="QuizSplashActivity"> </activity>
<activity android:name="QuizGameActivity"> </activity>
<activity android:name="QuizHelpActivity"> </activity>
<activity android:name="QuizMenuActivity"> </activity>
<activity android:name="QuizScoresActivity"> </activity>
<activity android:name="QuizSettingsActivity"> </activity>
</application>
</manifest>
For some reason, my App never shows up on the Emulator.
I've read the answers of other people asking the same question, but my App still does not show.
I do not get any Compilation or Running Errors.
If it's any help, my QuizActivity.java file is
package com.example.btdt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.TextView;
public class QuizActivity extends Activity {
TextView countDisplay;
public static final String GAME_PREFERENCES = "GamePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_splash);
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "Ryan D'souza");
prefEditor.putInt("UserAge", 16);
prefEditor.commit();
countDisplay = new TextView(this);
this.setContentView(countDisplay);
if(settings.contains("UserName") == true)
{
String user = settings.getString("UserName", "Default");
countDisplay.setText(user);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_splash, menu);
return true;
}
}
Thank you for your help
Try To declare your other activities like that:
<activity
android:name=".YourClass"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.your.package.name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If the emulator does not show it, restart Eclipse and open it. Before to run the app, open the emulator and wait still it load.
I'm trying to let a user press a button so it would vibrate. So i declared it in manifest and i made a new activity for it. Heres some code did i misplace the permissions?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.app"
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=".AndroidActivity"
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>
<activity android:name=".activity2"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".activity3"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".next" >
</activity>
<uses-permission android:name="android.permission.VIBRATE"/>
</application>
</manifest>
and heres the activity for the button:
package android.app;
import android.app.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidVibrator extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main3);
final Button button = (Button) findViewById(R.id.sound);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
Try the following -:
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application>
.
.
.
</application
First of all if you are developing in Eclipse use, ctrl+A and then ctrl+I this should correctly manage indentation for you. Returning to your question I hope you do replace names of activity1 and activity2 etc. with what you really have like "AndroidVibrator". And lastly it should make no difference where you place the permissions although it is a better practice to have them at the top , between uses-sdk and application.
Also I see you are using android:screenOrientation="portrait", if you want your activity not to react to screen orientation changes then add that too: android:configChanges="orientation|keyboardHidden|keyboard"
And add that to your activity class:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}