我在活动A然后从A开始活动B.现在我在活动B并从B开始活动C.在开始活动C时,我想删除活动A和B.我试过这种方式,
Intent intent = new Intent(B.this, C.class); //I'm on Activity B, moving to C
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //this should remove the Activity A
startActivity(intent);
finish(); //Finishes activity B
Run Code Online (Sandbox Code Playgroud)
我担心这样做,当我的活动C开始时,我按回来,应该退出应用程序.目前它向我展示了活动A.
当我广播挂起的意图并通过 提供我的数据时putExtra(),我在接收器中使用相同的数据获取这些意图,这等于第一个创建的挂起意图。代码 :
for (int i = 0; i < parts.size(); i++) {
mSendIntent=new Intent("CTS_SMS_SEND_ACTION");
mSendIntent.putExtra("key",i):
sentIntents.add(PendingIntent.getBroadcast(smscontext, 0, mSendIntent,
0));
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过向getBroadcast方法添加一些标志来获得结果,但没有成功,知道吗?
我想更新UI一个fragmentfrom service.我正在使用GCM发送消息.我有这个类GCM:
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (extras != null && !extras.isEmpty()) { // has effect of unparcelling Bundle
// Since we're not using two way messaging, this is all we …Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-service android-fragments google-cloud-messaging
我有以下问题:我编写了一个 android 服务 - 一个音乐播放器 - 在后台运行并在启动时启动通知。单击此通知会打开允许与该玩家互动的活动 A。这工作正常。
问题是当我有第三方活动(例如 Web 浏览器)运行时,我单击了通知。此单击将我带到活动 A,但单击“后退”按钮 - 这就是问题 - 将我带到主屏幕。相反,我想恢复以前运行的活动,即 Web 浏览器。
有人知道怎么做吗?
private void initNotification(){
Intent resultIntent = new Intent(this, AudioPlayerActivity.class);
//resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(AudioPlayerActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
stackBuilder.getIntents();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(...);
mBuilder.setContentTitle("...");
mBuilder.setContentText("...");
mBuilder.setContentIntent(resultPendingIntent);
// create a notification manager that then displays the notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud) android android-intent android-notifications android-activity android-pendingintent
我正在尝试制作一个按钮来更改 Android Lollipop 中的中断过滤器(无、优先级、全部)。当我按下按钮时,日志显示W/NotificationListenerService[NotificationService]: Notification listener service not yet bound.并且不会更改中断过滤器。我得到了"NLS Started"和"NLS Bound"日志。为什么它给我这个警告?这是我的代码:
MainActivity.java:
public class MainActivity extends Activity {
private NotificationService notifs;
private ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifs = new NotificationService();
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("NOTIF", "NLS Started");
NotificationService.ServiceBinder binder = (NotificationService.ServiceBinder)service;
notifs = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("NOTIF", "NLS Stopped");
}
};
Intent …Run Code Online (Sandbox Code Playgroud) 问题是,当我从深色模式切换到浅色模式(android 深色模式和 android 浅色模式)时,正在进行的活动工作正常,但重复的活动会重新开始。
现在,当您只处理视觉效果时,这并不重要,因为新活动覆盖屏幕并且旧活动不显示,但我也在处理声音,因此可以听到两个活动中播放的声音。
简而言之,当从深色模式切换到浅色模式(或反之亦然)时,正在进行的活动会继续,而同一活动的重复活动将从头开始。我不想要这个新的重复活动。我该如何避免呢?
我认为这里不需要我的代码。我需要 Java 的答案。
我看了2天以前发布的问题和答案,我已经想尽变化建议,以及在我的舱单,我launchMode属性设置为"标准".
我按下按钮后试图将第二个活动的数据传回我的第一个活动.按下按钮后,第一个活动启动但不会返回到我的onActivityResult()方法.我无法弄清楚为什么会这样.
这是我在活动2中的代码:
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Check that message is printed out to LogCat
System.out.println("hello test1 Activity2");
EditText band = (EditText) findViewById(R.id.txtBand);
band.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
EditText album = (EditText) findViewById(R.id.txtAlbum);
album.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
final Spinner genre = (Spinner) findViewById(R.id.spin_genre);
TextView selection = (TextView)genre.getSelectedView();
CharSequence strBand = band.getText();
CharSequence strAlbum = album.getText();
CharSequence strGenre = selection.getText();
//Check that we got input values
System.out.println("hello test Activity2- …Run Code Online (Sandbox Code Playgroud) 我的堆栈中有以下活动:
A , B , C, D
Run Code Online (Sandbox Code Playgroud)
我想重新启动B以获得此堆栈:
A , B'
Run Code Online (Sandbox Code Playgroud)
其中B'是一个新的B实例(不是旧的接收onNewIntent回调,我怎么能这样做?
顺便说一句,我正在使用
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)
在启动B活动时,但这样调用onNewIntent而不是启动新实例
好的,在这里忍受我
我的应用程序由闪屏活动(A)和主要活动(B)组成.当应用程序启动时,(A)显示一点,然后启动(B).之后(A)结束.这在"正常"条件下工作正常.这是启动的代码(B)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(A.this, B.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DELAY);
Run Code Online (Sandbox Code Playgroud)
当通知到达时,用户点击它.我是通过PendingIntent开始的(A):
Intent mIntent = new Intent(this, A.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon()... //build the whole notification
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(0, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
这开始于(A)然后是(B)并且一切都很好.
然而...
一旦应用程序在屏幕上显示并且第二个通知到达(A)不再启动,也不会在(B)中收到任何回调
阅读http://developer.android.com/guide/components/tasks-and-back-stack.html#ActivityState上的文档我得出结论,我应该使用FLAG_ACTIVITY_NEW_TASK设置启动(A)以便它启动一个新任务只有在(A)尚未运行),我也应该开始(B)与标志FLAG_ACTIVITY_SINGLE_TOP(这样我就可以得到一个回调B.onNewIntent() ,因为B将被运行).所以我做了
...
mainIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
....
mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
....
Run Code Online (Sandbox Code Playgroud)
但是,不,不.这种行为似乎根本没有改变.
我错过了文档中的内容吗?在我看来,每次在我的情况下FLAG_ACTIVITY_NEW_TASK应该开始(A),因为到第二个通知到达时.(A)已经完成,但它什么也没做.
你能给我一些关于如何获得回调的信息,这样我就能向用户显示正确的信息吗?
谢谢
我想使用广播接收器在每次活动中连续检查互联网连接。我已经编写了代码,它可以正常工作。但我想在每个活动中都使用它。如何修改此代码?
public class MainActivity extends Activity {
private static final String LOG_TAG = "CheckNetworkStatus";
private NetworkChangeReceiver receiver;
private boolean isConnected = false;
private TextView networkStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkChangeReceiver();
registerReceiver(receiver, filter);
networkStatus = (TextView) findViewById(R.id.networkStatus);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onDestroy() { …Run Code Online (Sandbox Code Playgroud) 我在使用小部件时看到奇怪的行为。我确信它以前曾起作用,但无论如何现在都没有。不知道我做了什么更改或它是否确实有效。下面的代码创建了我正在尝试做的简化版本。一个应用程序提供一个小部件。当它放在屏幕上时,提示用户输入数字。然后,该编号将显示在窗口小部件上。单击小部件将数字打印到日志中。可以使用不同的编号创建此窗口小部件的多个实例。单击每个将产生一个与为每个小部件选择的编号相对应的日志条目。
但是,我看到某种缓存行为。假设我创建了widget 3和5。单击3将记录3。单击5将记录3。删除3并单击5将再次记录3。这是如何运作的?看起来第一个窗口小部件已创建并且其视图已缓存?
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".AppApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.DashboardActivity"
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=".activity.DummyWidgetConfigActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<receiver android:name=".DummyWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/dummy_widget_info" />
</receiver>
<service
android:name=".service.DummyWidgetService"
android:exported="false" />
</application> …Run Code Online (Sandbox Code Playgroud) android android-widget android-service android-activity android-pendingintent
这是我的代码:
@Override
public void onCreate() {
super.onCreate();
//Log.i("TAG", "ovdje smo");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("alo", "bre");
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
oldCellID = cellLocation.getCid();
while( true ){
Log.i("jesmo li tu",""+oldCellID);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Toast.makeText(this, "Kupimo cell ID OLD" + oldCellID, Toast.LENGTH_SHORT).show();
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
newCellID = cellLocation.getCid();
if(oldCellID != newCellID){
//TODO uzmi lokaciju + spremi u bazu
Log.i("usli u petlju", …Run Code Online (Sandbox Code Playgroud) 今天,我遇到了一个关于共享数据进程间的奇怪问题。我声明MainActivity运行在另一个进程中,将TestApplication中的共享数据写为1,然后启动SubActivity来展示共享数据。不幸的是,SubActivity中显示的值仍然为0,因此我们得出结论,在两个进程中填充了两个TestApplication实例,并且共享数据的读写是相互独立的。实际上,共享数据不再在进程间共享。我的问题是在新进程中开始的活动与原始活动之间的另一个区别是什么,例如关于内存?这是我的代码:
<application
android:name=".TestApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:process="com.rlk.miaoxinli.hellokitty"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubActivity">
</activity>
</application>
public class TestApplication extends Application {
public int mValue = 0;
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private TestApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (TestApplication) getApplication();
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.first);
mApplication.mValue = 1;
mTextView.setClickable(true);
mTextView.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
mTextView.setText("" …Run Code Online (Sandbox Code Playgroud) android multiprocessing interprocess android-manifest android-activity