小编Car*_*ris的帖子

如何在意图之间传递布尔值

当按下后退按钮时,我需要将布尔值传递给intent,然后再返回intent.目标是设置布尔值并使用条件来防止在检测到onShake事件时多次启动新意图.我会使用SharedPreferences,但似乎它与我的onClick代码不相配,我不知道如何解决这个问题.任何建议,将不胜感激!

public class MyApp extends Activity {

private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mSensorListener = new ShakeEventListener();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_UI);


    mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

      public void onShake() {
             // This code is launched multiple times on a vigorous
             // shake of the device.  I need to prevent this.
            Intent myIntent = new Intent(MyApp.this, NextActivity.class);
            MyApp.this.startActivity(myIntent);
      }
    });

}

@Override
protected …
Run Code Online (Sandbox Code Playgroud)

android shake android-intent

18
推荐指数
2
解决办法
4万
查看次数

onClickListener中的AlertDialog

我正在尝试从onClickListener启动AlertDialog但是我收到以下错误.

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这一问题?

        mRecordButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder( this )
            .setTitle( "Cast Recording" )
            .setMessage( "Now recording your message" )
            .setPositiveButton( "Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Positive" );
                }
            })
            .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Negative" );
                }
            } )
            .show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

android android-alertdialog

13
推荐指数
1
解决办法
2万
查看次数

Android通知无效

我一直在尝试从ASyncTask成功上传通知,以便全天工作.我没有从我当前的代码中收到任何错误,但我无法通知通知栏(或其他任何地方).我在LogCat中没有收到任何消息,并且通知栏中没有显示任何通知.这是我的代码:

Notification mNotification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";

Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
Run Code Online (Sandbox Code Playgroud)

这是从ASyncTask的onPostExecute()方法调用的.老实说,我对PendingIntent部分感到有点困惑.任何澄清我怀疑是不正确的代码将非常感激.

android android-notifications android-asynctask

10
推荐指数
2
解决办法
1万
查看次数