Is it possible to get system vibration pattern as a long[] array? For example standard SMS vibration pattern or current selected in preferences SMS vibration signal.
I don't think it's possible to get the default patterns of your current device as a long[] array. You'll probably have to do a trial and error approach on this. And set a bunch of if's on your code to choose a different vibration pattern depending on the device or cellphone brand.
Something like:
YourActivity.java
import android.os.Vibrator;
private long[] patt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String man = android.os.Build.MANUFACTURER;
if (man.equals("SAMSUNG")) {
patt = {0l,300l,50l,350l};
}
else {
patt = {0l,200l,100l,100l};
}
vibrate(patt);
}
private void vibrate(long[] pattern) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (v.hasVibrator()) {
v.vibrate(pattern,-1);
}
}
And don't forget to add the permission to vibrate.
AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE"/>
Refer to this link for android.os.Build information.
Related
I have been looking at the new methods available for Accessibility in Android O. I ran across this new method called getAccessibilityButtonController, I am unsure precisely what it does and an intended use. I know that in Android O there is a navigation button that can be used for an accessibility service. Does this accessibility button only launch the accessibility service, or could it have other functionality within the service such as to do specific tasks? I am curious possible uses for the accessibility and the getAccessibilityButtonController methods. Thank you for your time.
It can do pretty much anything you want it to. From the android accessibility doc, the button allows you to register a callback that has an onClicked method. If you enable the button and provide said callback you can execute whatever you'd like in the context of that callback.
Edit: The android documentation has been updated so the following should no longer be necessary.
Note that if you read the doc there's currently an example that has a call to getAccessibilityButtonController() within onCreate(). This is incorrect because the controller isn't valid until onServiceConnected is called. I've modified the example below to show something that should work.
private AccessibilityButtonController mAccessibilityButtonController;
private AccessibilityButtonController
.AccessibilityButtonCallback mAccessibilityButtonCallback;
private boolean mIsAccessibilityButtonAvailable;
#Override
protected void onServiceConnected() {
mAccessibilityButtonController = getAccessibilityButtonController();
mIsAccessibilityButtonAvailable =
mAccessibilityButtonController.isAccessibilityButtonAvailable();
if (!mIsAccessibilityButtonAvailable) {
return;
}
AccessibilityServiceInfo serviceInfo = getServiceInfo();
serviceInfo.flags
|= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON;
setServiceInfo(serviceInfo);
mAccessibilityButtonCallback =
new AccessibilityButtonController.AccessibilityButtonCallback() {
#Override
public void onClicked(AccessibilityButtonController controller) {
Log.d("MY_APP_TAG", "Accessibility button pressed!");
// Add custom logic for a service to react to the
// accessibility button being pressed.
}
#Override
public void onAvailabilityChanged(
AccessibilityButtonController controller, boolean available) {
if (controller.equals(mAccessibilityButtonController)) {
mIsAccessibilityButtonAvailable = available;
}
}
};
if (mAccessibilityButtonCallback != null) {
mAccessibilityButtonController.registerAccessibilityButtonCallback(
mAccessibilityButtonCallback, null);
}
}
I've been trying out Google Tag Manager for mobile devices, specifically Android but I keep getting a message saying "invalid macro" when trying getString(myKeyValue) on a Container.
Here's a part of my code in my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtHello = (TextView)findViewById(R.id.txtHello);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String hello = mContainer.getString("hello");
long l = mContainer.getLong("long");
txtHello.setText(hello + l);
}
});
tagManager = TagManager.getInstance(this);
ContainerOpener.openContainer(tagManager, CONTAINER_ID, OpenType.PREFER_NON_DEFAULT, null, new ContainerOpener.Notifier() {
#Override
public void containerAvailable(Container container) {
mContainer = container;
}
});
}
I've added these permissions in the manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
I have the right Container_id because it shows the right version after refreshing it programmatically.
And this is my assets/tagmanager/CONTAINER_ID.json file (of course with the right filename):
{
'hello': 'hola',
'long' : '12345679'
}
So after my container is initialized, I press a button that runs the code above, trying to get the values. But I get the error: "Invalid macro: hello" and "Invalid macro: long", also
"Failed to convert '' to a number"
This is a new service for mobile devices but can anybody help me with this?
I've found a problem for my case. I just downloaded a version from the web browser manager.
The important thing is to add a rule that allows GTM to use this macro. Always comes in handy here.
Don't forget to publish the version of your container
For people not getting what dumazy referring to
you'll also need to enable it with Always or any other appropriate rules
In your json you are using '123456' quotas to define long, you should not:
{
'string': 'hola',
'long': 123456,
'double': 123.123
}
If you have further problems look on: Android TagManager not getting default values
I've been working on an android program. One portion of this program interacts with a webservice using a socket connection, sending files that on average are about 320 kB. The code ran on a desktop takes about 1.5 minutes to transfer. Using my android phone (Atrix) it seems to be taking about an hour. The phone is connected to wifi so I wasn't expecting it to take such a long time. My initial thought was to add a wifi lock, but it hasn't helped any.
I have the actual upload running in a async task (For reading I've made some of it pseudo)
#Override
protected void onPreExecute()
{
//Before starting the task show the uploading dialog
uploadingDialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
//After the task close the dialog
uploadingDialog.dismiss();
}
#Override
protected Boolean doInBackground(String... params) {
//Upload the files in the background
//keep track of upload results
boolean uploaded = true;
boolean temp;
//lock wifi on and stop the program from sleeping
_keepOnStart();
//Upload each file individually
for(int i=0; i <= fileNameList.size()-1; i++){
//this method does the actual writing to the socket/converts
//the file to a byte array etc.
temp = serverConnection.uploadWord(fileNameList.get(i));
if(temp == false) {
uploaded = false;
}
}
_keepOnStop();
return uploaded;
}
private void _keepOnStart() {
if (_powerManagement == null) {
_powerManagement = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
}
if (_wakeLock == null) {
_wakeLock = _powerManagement.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
"0 Backup power lock");
}
_wakeLock.acquire();
WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
_wifiLock = wifiManager.createWifiLock("0 Backup wifi lock");
_wifiLock.acquire();
}
}
private void _keepOnStop() {
if ((_wifiLock != null) && (_wifiLock.isHeld())) {
_wifiLock.release();
}
if ((_wakeLock != null) && (_wakeLock.isHeld())) {
_wakeLock.release();
}
}
On the desktop version of the code I was just timing "serverConnection.uploadWord(fileNameList.get(i));" with a set file name.
The method itself grabs the byte data from the file, creates a packet to send to the server and then sends it out.
Some of my manifest permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I'm wondering if anyone can provide an explaination for this. My assumption is that the device is using it's data connection, but at the same time only background data is allowed on the device, and I see no data use in the last 7 days.
(any and all help is much appreciated. If I'm unclear in anyway please let me know.)
For anyone looking at the same thing. It seems this is the correct approach. The immense amount of time was a derivative of a very inefficient encoding scheme that was done before sending the data. (It did not scale well)
i want to set the system preference (by code) for just using 2g networks instead of using 3g. so far i haven't found anything that could have helped me. i suppose i need to set it via the ConnectionManager? can anyone point me in the right direction here?
Unfortunately, you can't do this. The best you can do is take the user to the relevant settings screen (Mobile Network Settings), where they can choose for themselves. There's no API to actually change the setting.
Some ROMs (e.g. CyanogenMod) build this into the system, and there may be options if you are rooted/installed as a system app, but if you want something standard/mass applicable then I'm afraid you're out of luck.
use the following code
public class CheckNetworkType extends Activity
{
private static final String tag = "CheckNetworkType";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE )
{
// Network type is 2G
Log.v(tag, "2G or GSM");
}
else
if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_CDMA)
{
// Network type is 2G
Log.v(tag, "2G or CDMA");
}
else
if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS)
{
// Network type is 3G
Log.v(tag, "3G Network available.");
}
}
}
In the Android app that I'm working on, I'd like to be able to detect when a new status bar notification appears, regardless of if it was caused by my app. To be more specific, I want to count the number of notifications in a given time frame.
Is this even possible, and if so, how?
Actually, it is possible, I use it in my app.
For Android 4.2 and below:
You need to register an AccessibilityService and make sure the user enables the service.
Example for a service:
public class InstantMessenger extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
//Do something, eg getting packagename
final String packagename = String.valueOf(event.getPackageName());
}
}
#Override
protected void onServiceConnected() {
if (isInit) {
return;
}
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
setServiceInfo(info);
isInit = true;
}
#Override
public void onInterrupt() {
isInit = false;
}
}
Example for checking if your Service is activated
For Android 4.3 and above:
Use the Notification Listener API
The new Notification Listener API in Android 4.3 enables you to do this.
With this there is less need for the accessibility hack. It also allows you to dismiss notifications.